来自非 SQL 背景,我创建了一个简单的表,名为 test,有 3 个字段:
NSString *testtable = @"create table if not exists test(testid integer primary key, userid integer, contentid text)";
if (![db executeUpdate:testtable]) {
NSLog(@"create test, %d:%@", [db lastErrorCode], [db lastErrorMessage]);
return NO;
}
当我向表中插入一条记录时,出现 exc_bad_access 错误:
if (![db executeUpdate:@"insert into test values(?,?,?)", NULL, 1, @"HELLO"]) {
NSLog(@"insert test, %d:%@", [db lastErrorCode], [db lastErrorMessage]);
return NO;
}
如果我将列 'userid' 类型从整数更改为文本,这将运行良好。我在控制台上使用 sqlite 命令测试表,它也运行良好。
你们有什么感想?谢谢...
我尝试另一种方法来处理这个问题:
if (![db executeUpdate:@"insert into test values(?,?,?)", NULL, [NSNumber numberWithInt:1], @"HELLO"]) {
NSLog(@"testint, %d:%@", [db lastErrorCode], [db lastErrorMessage]);
return NO;
}
那么它的工作原理。我说不出原因……也许 fmdb 只支持对象插入。
最佳答案
我相信 FMDB 需要对象来替换 ?和 int 不是对象。将 1 更改为 [NSNumber numberWithInt:1]
另外,你在哪里打开你的数据库,你有这样的东西?
db = [FMDatabase databaseWithPath:writableDBPath];
if ([db open]) {
添加
[db setTraceExecution:TRUE];
紧随其后,它会为您记录您的 SQL 文本。可以先创建您的 SQL NSString,然后插入您的 int,然后让 FMDB 运行您已经填充的字符串。
关于insert - FMDB sqlite 插入失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10602046/