FMDB sqlite插入失败
发布时间:2020-12-12 18:54:09 所属栏目:百科 来源:网络整理
导读:来自非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
来自非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,[db lastErrorMessage]); return NO; } 如果我将列’userid’类型从整数更改为文本,这将很有效.我在控制台上使用sqlite命令测试表,它也运行良好. 我尝试另一种方法来处理这个问题: if (![db executeUpdate:@"insert into test values(?,[NSNumber numberWithInt:1],@"HELLO"]) { NSLog(@"testint,[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运行已填充的字符串. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |