1
我有兩個表中的第二個表有一個外鍵定義爲表1的主鍵。在添加操作下,如何引用新添加的表1鍵作爲表2的外鍵?獲取鍵SQLite Android
// Project Table columns names
private static final String KEY_ID = "id"; // Primary, integer
private static final String KEY_NAME = "name"; // Unique, text
// Images Table column names
private static final String KEY_IM_ID = "id"; // Primary, integer
private static final String IM_URI = "uri"; // text
private static final String PROJ_IMAGE_ID = "proImgId"; // foreign for Projects Table
public void addProject(Project project) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values_PT = new ContentValues(); // PROJECTS TABLE
ContentValues values_IT = new ContentValues(); // IMAGES TABLE
values_PT.put(KEY_NAME, project.getName()); // project name
// does the KEY ID get added on its own?
values_IT.put(IM_URI, project.getURI()); // image uri
values_IT.put(PROJ_IMAGE_ID, ????????);
}
PROJ_IMAGE_ID需要有KEY_ID的值,但我不知道如何檢索它。有沒有辦法做到這一點?
啊好吧,所以我會把它存儲在一些長期價值?例如: 'Long keyId = db.insert(PROJECT_TABLE,null,values_PT);' –
是(或者如果您的記錄少於40億,則爲'int')。 –