我正在使用下面的代码来更新查询sqlite
。
但我越来越"database is locked error"
。
我尝试搜索一些 SO 链接,并建议关闭数据库,但我再次这样做时遇到了同样的错误。我已经提到我在代码中遇到错误的地方。
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);
}
}