FMDB sqlite插入失败

来自非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运行已填充的字符串.

点赞