sql - "database locked" error in ios while updating query -
i using below code to update query using sqlite
.
getting "database locked error"
.
tried searching link , suggested close database, did again getting same error. have mentioned getting error in code.
const char *dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &database) == sqlite_ok) { nsstring *locationno =null; nsstring *querysql = [nsstring stringwithformat:@"select count(*) 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 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 below lines ========================= const char *sq1l = "update code set code_1=?, code_2=?, code_3=?, code_4=?, code_5=?,code_6=? code_id=1"; if (sqlite3_prepare_v2(database, sq1l, -1, &statement, null) != sqlite_ok) { nslog(@"error: failed prepare statement 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 prepare statement message '%s'.", sqlite3_errmsg(database)); //result = false; } else { nslog(@"error: failed prepare statement message '%s'.", sqlite3_errmsg(database)); //result = true; } =================================end========================================= } sqlite3_reset(statement); } else { nslog(@"not found"); locationno=@"1"; } sqlite3_reset(statement); } }
generally if have multiple queries going on @ same time (either didn't finalize sql statement, or have multiple threads open, or you've opened database multiple times).
this code has confusing use of sqlite3_close
, sqlite3_reset
(and lack of sqlite3_finalize
), might source of problem.
in an introduction sqlite c/c++ interface, point out correct order of statements:
sqlite3_open()
, open databasesqlite3_prepare()
, prepare sql statementsqlite3_bind()
, bind values ? placeholders necessarysqlite3_step()
, execute sql and/or step through resultssqlite3_column()
, retrieve columns of data, necessarysqlite3_finalize()
, complete/close prepared sql statementsqlite3_close()
, close database
bottom line, sqlite3_open
call not matched single sqlite3_close
statement @ end (but have extraneous sqlite3_close
in middle of code). also, each sqlite3_prepare_v2
must have own sqlite3_finalize
(you use sqlite3_reset
if want reset prepared statement can bind new values , step through again; still need sqlite3_finalize
when you're done prepared statement).
Comments
Post a Comment