iphoneobjective-csqlite

Should I use sqlite3_finalize after i performed a query with sqlite3_exec?


When using Sqlite3 in my iPhone app, I was getting some unwanted rollbacks, apparently on a random basis. However, I don't know if this has something to do with the fact that I don't finalize the statements with sqlite3_finalize, since as far as I know sqlite3_exec takes care of it.

Also, I found some SELECTs with sqlite3_prepare_v2 that I didn't finalize, so I know I must finalize these, however should I do the same with the ones in sqlite3_exec?

One example of my statements is:

NSString *query = @"UPDATE books SET title='newName' WHERE id='21';";
if ((result=sqlite3_open([database UTF8String], &_database)) == SQLITE_OK) {
    result=sqlite3_exec(_database, [query UTF8String], NULL, NULL, &errorMsg);
    if (result != SQLITE_OK) {
        printf("\n%s", errorMsg);
        sqlite3_free(errorMsg);
    }
    sqlite3_close(_database);
}

Should I sqlite3_finalize(result) before closing the database?


Solution

  • No. You don't need, because sqlite3_finalize() function is called to delete a prepared statement which is created by using sqlite3_prepare_v2() or a related function.