1

why wouldn't there exist a SQLite database after the app was updated to a new version ? How is it possible that the database is no longer present even though it did exist earlier.

(using Android 2.3.3)

Error Log why would SQLiteOpenHelper.getWritableDatabase() cause this exception? Surely it would be clever enough to realize "oooh the db doesn't exist, let's create it!

note that the new SQLiteOpenHelper(context) worked OK as far as I can tell. However, I do not know whether onCreate() or onUpgrade() were triggered.

06-04 18:21:01.706 D/AndroidRuntime( 1280): Shutting down VM
06-04 18:21:01.706 W/dalvikvm( 1280): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-04 18:21:01.745 E/AndroidRuntime( 1280): FATAL EXCEPTION: main
06-04 18:21:01.745 E/AndroidRuntime( 1280): java.lang.ExceptionInInitializerError
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at myapp.com.MyActivity.onCreate(MyActivity.java:131)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.os.Looper.loop(Looper.java:130)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at java.lang.reflect.Method.invokeNative(Native Method)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at java.lang.reflect.Method.invoke(Method.java:507)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at dalvik.system.NativeStart.main(Native Method)
06-04 18:21:01.745 E/AndroidRuntime( 1280): Caused by: android.database.sqlite.SQLiteException: unable to open database file
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1849)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:547)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at myapp.com.storage.SQLiteStorage.<init>(SQLiteStorage.java:61)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     at myapp.com.storage.SQLiteStorage.<clinit>(SQLiteStorage.java:58)
06-04 18:21:01.745 E/AndroidRuntime( 1280):     ... 14 more
06-04 18:21:01.776 W/ActivityManager( 1002):   Force finishing activity myapp.com/.MyActivity

After I trap the exception "SQLiteException: unable to open database file", should the app simply try to instantiate another SQLiteOpenHelper ?

4

1 に答える 1

1

I've gotten it working reliably (test case was to force close the app, delete the .db file it created, then restart the app - this requires a rooted phone to test).

Solution:

the SQLiteOpenHelper object (nested class within my SQLiteStorage singleton class) needed to be instantiated from Application.onCreate() so that the tables were ready by the time the service and launcher activity called their onCreate()'s

Explanation:

What happened is the launcher activity started the service through an intent (which also used the SQLite DB for storage) and the launcher activity also initiated the SQLiteOpenHelper.onCreate(). However, because there's a lot of tables that needed to be created there existed a race condition and sometimes the database wasn't ready by the time the service wanted it.

于 2012-06-06T21:45:12.610 に答える