Android Database Locked

I think you have forgotten to close the database, or another thread is writing to the database when you are trying to write to it. SQLite locks the database when it is writing to it to avoid corruption if another entity tries to write to the same database at the same time. Android, will only show a error in log cat, and the query you supplied will be just forgotten...

So, I recommend:

  • You only access the database from one SQLOpenHelper
  • You make sure you close all instances of database helpers once you have finished with them
  • You make sure you always end transactions with endTransaction() also if you do not set them successful (i.e. if you want to roll 'em back), in case you use transactions
  • You could try using OrmLite, I've not used it, but I've heard others here rave about it.

The reason this is happening is because you can only have one connection to the database open at a time (this is to prevent race conditions when modifying the database). I'm guessing your connecting multiple times to your database from different threads? The easiest way to get around this is to just create a ContentProvider frontend for your database. This will be able to handle queries from multiple threads.

For more information, checkout this blogpost.


Use a single connection throughout your whole app. See here:

http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection

Tags:

Android