gpt4 book ai didi

ios - SQLite id 字段 - 自动填充

转载 作者:行者123 更新时间:2023-11-29 02:23:37 25 4
gpt4 key购买 nike

我已经在我的数据库中添加了一个新表:

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

现在的问题是如何插入数据使这个字段自动插入myId?我不确定我的插入代码是否无效或 CREATE TABLE 语句。

-- 更新--

我已经更新了 create 语句以更正一个:

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
);

最佳答案

关于您的评论,您需要在 QUERY 中使用 NULL 值指定 PRIMARY KEY:

[db executeUpdate:@"INSERT INTO myTable VALUES(?,?,?,?)",
NULL,
@(aNumber),
myData.anUniqueTextId,
myData.aText];

这样数据库将填充主键并自动增加它(PRIMARY KEY 的 SQL Lite 规范)。

关于ios - SQLite id 字段 - 自动填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com