更新查询时,ios中的“数据库locking”错误
我正在使用下面的代码来更新使用sqlite
的查询 。
但是我得到"database is locked error"
。
我试图search一些SO链接,并build议closures数据库,但我又这样做了同样的错误。 我已经提到了在代码中出现错误的地方。
const char *dbpath = [databasePath UTF8String]; if (sqlite3_open(dbpath, &database) == SQLITE_OK) { NSString *locationNo =NULL; NSString *querySQL = [NSString stringWithFormat:@"select count(*) from code"]; const char *query_stmt = [querySQL UTF8String]; if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_step(statement) == SQLITE_ROW) { locationNo = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]; int count= [locationNo intValue]; sqlite3_close(database); NSLog(@"%@",locationNo); if(0==count) { NSString *insertSQL = [NSString stringWithFormat:@"insert into favourite_code (code_id,code_1,code_2,code_3,code_4,code_5,code_6, status, record_status) VALUES (\"%d\",\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",1 ,code1,code2,code3,code4,code5,code6,@"Y", @"Y"]; const char *insert_stmt = [insertSQL UTF8String]; sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL); if (sqlite3_step(statement) == SQLITE_DONE) { return YES; } else { return NO; } sqlite3_reset(statement); sqlite3_close(database); } else{ =========================== Getting Error in the below lines ========================= const char *sq1l = "update code SET code_1=?, code_2=?, code_3=?, code_4=?, code_5=?,code_6=? WHERE code_id=1"; if (sqlite3_prepare_v2(database, sq1l, -1, &statement, NULL) != SQLITE_OK) { NSLog(@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database)); } else { sqlite3_bind_text(statement, 1, [code1 UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(statement, 2, [code1 UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(statement, 3, [code1 UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(statement, 4, [code1 UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(statement, 5, [code1 UTF8String], -1, SQLITE_TRANSIENT); sqlite3_bind_text(statement, 6, [code1 UTF8String], -1, SQLITE_TRANSIENT); } int success = sqlite3_step(statement); if (success != SQLITE_DONE) { NSLog(@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database)); //result = FALSE; } else { NSLog(@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database)); //result = TRUE; } =================================END========================================= } sqlite3_reset(statement); } else { NSLog(@"Not found"); locationNo=@"1"; } sqlite3_reset(statement); } }
一般来说,如果你有多个查询同时进行(或者你没有完成一些早期的SQL语句,或者你有多个线程打开,或者你已经多次打开数据库),你会得到这个。
这个代码有一个使用sqlite3_close
和sqlite3_reset
(以及缺lesssqlite3_finalize
),这可能是问题的根源。
在SQLite C / C ++接口介绍中 ,他们指出了这些语句的正确顺序:
-
sqlite3_open()
,打开一个数据库 -
sqlite3_prepare()
,准备一个sql语句 -
sqlite3_bind()
,将值绑定到? 占位符 -
sqlite3_step()
,执行sql和/或步骤的结果 -
sqlite3_column()
,根据需要检索数据列 -
sqlite3_finalize()
,完成/closures准备好的sql语句 -
sqlite3_close()
,closures数据库
底线,您的sqlite3_open
调用与最后一个sqlite3_close
语句不匹配(但您的代码中间有一个额外的sqlite3_close
)。 另外,每个sqlite3_prepare_v2
必须有自己的sqlite3_finalize
(如果你想重置一个准备好的语句,你只能使用sqlite3_reset
,所以你可以用新的值绑定它,然后重新执行;但是当你完成准备工作时,你仍然需要sqlite3_finalize
声明)。
- 用xcodedebugging“无法及时启动”
- UITableView和presentViewController需要2次点击才能显示
- 如何从URLscheme中获取参数。
- 以编程方式在Swift中模拟UITableViewController中的select
- iOS – 如何访问设备的文件库?
- NSInternalInconsistencyException – 无法在捆绑中加载NIB
- 在iOS 9.3 / Xcode 7.3中使用StoreKit常量时使用未parsing的标识符
- 如何在我的自定义swift框架中集成Firebase cocoaPods?
- iOS Facebook SDK:必须使用活动访问令牌来查询有关当前用户的信息