Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode. Can't insert data [duplicate]

Tags:

android

I am getting following error. So how do I unlock my database?

10-28 08:43:26.510: ERROR/AndroidRuntime(881): FATAL EXCEPTION: Thread-11
10-28 08:43:26.510: ERROR/AndroidRuntime(881): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:742)
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at com.aa.me.vianet.dbAdapter.DbAdapter.open(DbAdapter.java:25)
10-28 08:43:26.510: ERROR/AndroidRuntime(881):     at com.aa.me.vianet.services.NotificationManagerThread.run(NotificationManagerThread.java:49)
like image 293
user533844 Avatar asked Apr 30 '26 21:04

user533844


2 Answers

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.
like image 95
Todd Davies Avatar answered May 02 '26 13:05

Todd Davies


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

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

like image 39
Kevin Galligan Avatar answered May 02 '26 13:05

Kevin Galligan



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!