c++ - Databse getting locked when trying to update and insert -
when try insert or update, database gets locked randomly. giving code using. kindly let me know if else needs done apart this.
i read issue occurs because of open , close functionality of database. please let me know issue is?
-(bool)createdb : (const char *)str { nsstring *docsdir; nsarray *dirpaths; // documents directory dirpaths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); docsdir = dirpaths[0]; // build path database file databasepath = [[nsstring alloc] initwithstring:[docsdir stringbyappendingpathcomponent: @"spotlogic.db"]]; bool issuccess = yes; // nsfilemanager *filemgr = [nsfilemanager defaultmanager]; dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &database) == sqlite_ok) { char *errmsg; const char *sql_stmt = str; if (sqlite3_exec(database, sql_stmt, null, null, &errmsg) != sqlite_ok) { issuccess = no; nslog(@"failed create table"); } sqlite3_close(database); return issuccess; } else { issuccess = no; nslog(@"failed open/create database"); } return issuccess; } - (bool)deletedata:(nsstring *)deletesql { nsstring *docsdir; nsarray *dirpaths; // documents directory dirpaths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); docsdir = dirpaths[0]; // build path database file databasepath = [[nsstring alloc] initwithstring:[docsdir stringbyappendingpathcomponent: @"spotlogic.db"]]; dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &database) == sqlite_ok) { const char *insert_stmt = [deletesql utf8string]; sqlite3_prepare_v2(database, insert_stmt,-1, &statement, null); if (sqlite3_step(statement) == sqlite_done) { return yes; nslog(@"deleted"); } else { nslog(@"not deleted"); return no; } sqlite3_reset(statement); } return no; } - (bool)insertdata:(nsstring *)insertsql { nsstring *docsdir; nsarray *dirpaths; // documents directory dirpaths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); docsdir = dirpaths[0]; // build path database file databasepath = [[nsstring alloc] initwithstring:[docsdir stringbyappendingpathcomponent: @"spotlogic.db"]]; dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &database) == sqlite_ok) { const char *insert_stmt = [insertsql utf8string]; sqlite3_prepare_v2(database, insert_stmt,-1, &statement, null); if (sqlite3_step(statement) == sqlite_done) { return yes; nslog(@"inserted"); } else { nslog(@"not inserted"); return no; } sqlite3_reset(statement); } return no; } - (sqlite3_stmt *)fetchdata:(nsstring *)fetchsql { nslog(@"%@",fetchsql); nsstring *docsdir; nsarray *dirpaths; // documents directory dirpaths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); docsdir = dirpaths[0]; // build path database file databasepath = [[nsstring alloc] initwithstring:[docsdir stringbyappendingpathcomponent: @"spotlogic.db"]]; // array = [[nsmutablearray alloc]init]; dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &database) == sqlite_ok) { const char *fetch_stmt = [fetchsql utf8string]; if(sqlite3_prepare_v2(database, fetch_stmt,-1, &statement, null)==sqlite_ok) { sqlite3_reset(statement); } else { } } return statement; } - (bool)update:(nsstring *)insertsql { nsstring *docsdir; nsarray *dirpaths; // documents directory dirpaths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); docsdir = dirpaths[0]; // build path database file databasepath = [[nsstring alloc] initwithstring:[docsdir stringbyappendingpathcomponent: @"spotlogic.db"]]; dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &database) == sqlite_ok) { const char *insert_stmt = [insertsql utf8string]; sqlite3_prepare_v2(database, insert_stmt,-1, &statement, null); if (sqlite3_step(statement) == sqlite_done) { return yes; nslog(@"inserted"); } else { nslog(@"not inserted"); return no; } sqlite3_reset(statement); } return no; }
i found problem. database gets locked when run code.
for(int = 0 ; < meeting_ids.count ; ++){ statement = [[datamanger getsharedinstance] fetchdata:[nsstring stringwithformat:@"select red_flag meeting_question meeting_id = '%@'",[meeting_ids objectatindex:i]]]; while (sqlite3_step(statement) == sqlite_row) { char *field1=(char *) sqlite3_column_text(statement, 0); nsstring *name =[[ nsstring alloc]initwithutf8string:field1]; nslog(@"%@",name); if([name isequaltostring:@"1"]){ [red_flag replaceobjectatindex:i withobject:@"1"]; break; } } }
this while loop reason. have 'red_flag' meetings meeting ids in 'meeting_ids' array. other way this
try this:
if opens database every time, close every time.
so, write ...
sqlite3_close(database);
this line in function.
Comments
Post a Comment