Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 253
Leg10n Avatar asked Feb 12 '26 18:02

Leg10n


1 Answers

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.

like image 194
pierrotlefou Avatar answered Feb 14 '26 14:02

pierrotlefou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!