开发者

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

I'm using Sqlite3 in my iPhone app, I was getting some unwanted rollbacks apparently in 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 UTF8St开发者_开发百科ring],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?


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.


No need to call sqlite3_finalize() when working with sqlite3_exec. This is because sqlite3_exec is a shortcut for 3 functions:

  1. sqlite3_prepare_v2()
  2. sqlite3_step()
  3. sqlite3_finalize()

You can also read about this in a small sqlite tutorial at: http://www.iosdevelopment.be/sqlite-tutorial/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜