我添加了一個新的表到我的DB:SQLite的ID字段 - 自動填充
CREATE TABLE myTable (
myId ID NOT NULL,
aNumber INTEGER NOT NULL,
anUniqueTextId TEXT NOT NULL,
aText TEXT,
PRIMARY KEY(myId, anUniqueTextId) ON CONFLICT IGNORE,
FOREIGN KEY(anUniqueTextId, aNumber) REFERENCES otherTable(otherTableId, otherTableValue2) ON DELETE CASCADE
);
現在我想無需手動找出哪些索引中插入值應我把myId
領域:
[myDatabase inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"INSERT INTO myTable VALUES(?,?,?)",
// @(myData.myId), //I don't want to insert it manually
@(aNumber),
myData.anUniqueTextId,
myData.aText];
if (db.lastErrorCode) {
NSLog(@"Huston, we've got a problem: %@", db.lastError.localizedDescription);
}
}];
OFC,我來到這裏的錯誤說服力:
table myTable has 4 columns but 3 values were supplied
現在的問題是如何將數據插入到m ake這個字段autoinsert myId?我不確定我的插入代碼是無效的還是CREATE TABLE語句。
- 更新 -
我更新的創建語句來糾正一個:
CREATE TABLE myTable (
myId INTEGER PRIMARY KEY DEFAULT 0 NOT NULL,
aNumber INTEGER NOT NULL,
anUniqueTextId TEXT NOT NULL,
aText TEXT,
FOREIGN KEY(anUniqueTextId, aNumber) REFERENCES otherTable(otherTableId, otherTableValue2) ON DELETE CASCADE
);
這不適用於FMDB,它在類中的某處崩潰。 – Hemang