mardi 2 décembre 2014

How to use sqlite3_exec instead of sqlite3_prepare


Vote count:

0




In my app I am opening and closing the database several times at different places. Everywhere I used sqlite3_prepare . Initially it worked fine,but later I am getting database locked error. I want to use sqlite3_exec everywhere instead of sqlite3_prepare. Is this a good way to use sqlite?


Here is the code I used to open and close the database. I used this like around 5-6 times to perform different functions.



sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) {
if (sqlite3_prepare_v2(myDatabase, [query UTF8String], -1, &statement, nil)==SQLITE_OK)
{
if (sqlite3_step(statement)==SQLITE_ROW)
{
recordExist=YES;

}
else
{
NSLog(@"%s,",sqlite3_errmsg(myDatabase));
}

sqlite3_finalize(statement);

}
sqlite3_close(myDatabase);
}


Now, If I want to use sqlite3_exec, I tried this



int myCallback(void *a_parm, int argc, char **argv, char **column)
{
return 0;
}

-(void)myMethod{


sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) {
if (sqlite3_exec(myDatabase, [query UTF8String], myCallback,nil, nil)==SQLITE_OK)// is this the correct way to use the sqlite3_exec ?
{
if (sqlite3_step(statement)==SQLITE_ROW)
{
recordExist=YES;

}
else
{
NSLog(@"%s,",sqlite3_errmsg(myDatabase));
}

sqlite3_finalize(statement);

}
sqlite3_close(myDatabase); //do i need place close statement here ?
}
//or do i need to place close statement here?
}


I am also confused whether the finalize statement and close statement should be placed together or finalize should be used at the end of the sqlite3_step and close should be used at the end of the sqlite3_open. I am pretty confused about this. Kindly suggest me the proper way to use sqlite to perform operations frequently in different methods.



asked 2 mins ago

T_77

233






How to use sqlite3_exec instead of sqlite3_prepare

Aucun commentaire:

Enregistrer un commentaire