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

Exception or error:

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)
How to solve:

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.

###

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.

###

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.

###

@yuku

This kind of issue i have faced, I have solved this issue like that way.

  1. We need check whether Old database is same as New Database, If something changed into new Database that time we need delete Old database and install new database OR alter the table as per new Database

  2. Second option is easy just uninstall your old application and than install new application

Hope this will work for you..!!!

###

For any database application , I create the sqlite database first with Sqlite Manager addon of Firefox and take it in /res/raw folder. Then in my code, I check if the database exists in /data/data for this app. If not , I copy the db file to that directory with my code.

For your situation, it’s hard to determine the exact reason without seeing how you have created your database and how are you using it.There can be several reason for the issue of getting this exception. Some issues already mentioned by others. I want to mention a one. Can you try opening the database like this

SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);  

Probably, I could try better to help if you show your code snippets. Well, my methods are described here , you can take a look if that helps.

Populating SQLite Database

###

I have faced this issue.

One of my applications was behaving in the same way, because I added the code to copy database from assets to specified location on the splash screen (the screen which is visible when app launches and automatically goes off the view after few seconds).

And what was happening in few cases, that database was not getting copied in the specified time frame and accessing it from within the app was causing exceptions.

What I did,

I just wrote the code to copy database on background thread and show the splash screen to the user till the time database is not copied only after that show the next screen.

There could be another solution in case, if the app is being installed on previous version, write the code to copy the saved data from previous version of app to new vesrion if data prevention is necessary and new version contains different models than that of previous version.

###

Reinstall application and try to use synchronized block for database CURD operations 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *