Thursday, July 9, 2009

Sqlite and 'Database is locked'

For some reason I started seeing a few 'Database is locked' errors during development yesterday. This error was showing up when I tried to do an update/change on a row. This was really perplexing to me because I had used this kind of update and select code regularly before. Why the problem now? The error seemed to show up more frequently if I had done a large number of select statements prior to doing the update. But that of course doesn't make sense because as we all learned in our DB classes in college, reads do not cause database locking.

It took quite a bit of looking to finally find something that pointed me in a good direction. A user posted here to check out the SqliteBooks sample app from Apple. http://www.iphonedevsdk.com/forum/iphone-sdk-development/14106-sqlite-database-locked.html

It turns out that finding a link to download an SDK 2.x sample app is quite difficult. But I digress. The SQLiteBooks sample app was very educational.

It turns out that I had learned to do sqlite from one of the books I bought when I first started this journey. I think that somewhere in my cut-and-paste journey I had lost the sqlite3_finalize statement that should come at the end of an sqlite sequence (read or write). I added it back all over the place and all my locking pain went away.

2 comments:

  1. Thanks for the "sqlite3_finalize" hint.
    Now it works!

    ReplyDelete
  2. Excelent, i have the same problem and with sqlite3_finalize i resolve the problem.

    ReplyDelete