23

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)
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
  • make sure your database name inside assets folder and declare database name in the database helper class is same. and check the path of your database with database name while you trying to copy database. put your code here so that other can check your database helper class. – Maulik Oct 15 '13 at 12:37
  • Error shows that database could not open database, that means may be you have not set property "SQLiteDatabase.OPEN_READWRITE" condition in the open database method and also change into properties of your database file change read write permission. make sure above both cases. – Maulik Oct 15 '13 at 12:42
  • visit this [link](http://stackoverflow.com/questions/4187631/unable-to-open-database-file-when-using-sqliteopenhelper-with-instrumentation) may it will be help full for you – Engineer Oct 21 '13 at 06:52

7 Answers7

5

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.

diexsie
  • 77
  • 1
  • 4
2

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.

Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
2

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.

Vladimir Kroz
  • 5,237
  • 6
  • 39
  • 50
  • I also got same issue. I save my memo in a db. When I insert one new entry and press 'back' or 'home' key to back to home screen. After that I try to use backup agent to restore my memo, I always got same error (error code 14). If I kill the memo process, then my memo backup agent can work normally. I think the db is opened by another thread. How can I close it? Anyone could provide solution? – Joe Ho Feb 11 '11 at 11:39
0

@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..!!!

Indra
  • 528
  • 2
  • 11
0

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

Community
  • 1
  • 1
ayon
  • 2,180
  • 2
  • 17
  • 32
0

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.

Bette Devine
  • 1,196
  • 1
  • 9
  • 23
-1

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

Vinayak
  • 6,056
  • 1
  • 32
  • 30