Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random exception android.database.sqlite.SQLiteException: unable to open database file

My app uses a uncaught exception handler that sends the stack trace to me when the app crashes. Often I get this report from random users.

I cannot replicate it, the opening of the database always succeeds in my case. This is not a database stored on external SD card, only a database opened with SQLiteOpenHelper(context, "SomeName", null, someVersionCode).

Do you have any experience with this? What are the possibilities that I can check before opening the database?

Thank you!

android.database.sqlite.SQLiteException: unable to open database file
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4363)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: unable to open database file
    at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1698)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:761)
    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:754)
    at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:476)
    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
like image 221
Randy Sugianto 'Yuku' Avatar asked Aug 25 '10 07:08

Randy Sugianto 'Yuku'


3 Answers

i got the same error after upgrading my android firmware. the database was created by the old firmware and therefore couldn't be opened by the new firmware. users could solve this error by uninstall and reinstall your app.

like image 105
diexsie Avatar answered Oct 18 '22 12:10

diexsie


One of the possible scenarios when this could happen -- is when you access your database file from several threads and when the file is locked by one of the threads while you're trying to open it for modifications from another thread.

like image 23
Vladimir Kroz Avatar answered Oct 18 '22 12:10

Vladimir Kroz


there can be if your app opens database from multiple threads at same time that can be the reason for this exception.

please try synchronized method to open database.synchronized method will not allow database to be open from multiple threads at same time. put below code in your dbHelper

private synchronized SQLiteDatabase getWritableDB() {
        //get your database and return it from this method.
}

and call these method when your are getting db. Hope it will help.

like image 2
Nirav Tukadiya Avatar answered Oct 18 '22 14:10

Nirav Tukadiya