开发者

Reopen SQLiteOpenHelper when my application is stopped as "close()" was never explicitly called on database"

开发者 https://www.devze.com 2023-04-07 22:24 出处:网络
I\'m using the SQLiteOpenHelper. When my application is normally closed, the database is closed normally in the method onDestroy(), too. When it\'s force closed, the database is left open and later, w

I'm using the SQLiteOpenHelper. When my application is normally closed, the database is closed normally in the method onDestroy(), too. When it's force closed, the database is left open and later, when my application tries to read the database using getReadableDatabase(), it crashe开发者_运维百科d with the following message:

09-26 13:45:30.995: ERROR/Database(12632): close() was never explicitly called on database

Here is the full LogCat:

09-27 03:10:04.331: ERROR/Database(391): close() was never explicitly called on database '/data/data/myApplication/databases/DownloadDatabase1' 
09-27 03:10:04.331: ERROR/Database(391): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-27 03:10:04.331: ERROR/Database(391):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
09-27 03:10:04.331: ERROR/Database(391):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
09-27 03:10:04.331: ERROR/Database(391):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
09-27 03:10:04.331: ERROR/Database(391):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
09-27 03:10:04.331: ERROR/Database(391):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:544)
09-27 03:10:04.331: ERROR/Database(391):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
09-27 03:10:04.331: ERROR/Database(391):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
09-27 03:10:04.331: ERROR/Database(391):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)

I followed the stack trace and I guess the exception is because of the following line of code:

setLocale(Locale.getDefault());

Th code above is from the file SQLiteDatabase.java. Any suggestions on how to solve this problem?


Even though it gives that error, you should be able to access the database. Looking at the underlying code, my understanding is that such an error shouldn't cause your program to exit: http://hi-android.info/src/android/database/sqlite/SQLiteDatabase.java.html

Check the finalize() method there. You will see, it just tries to clean up, it doesn't throw exception.

You must have a problem somewhere else.


I would recommend closing the database as soon as you are done working with it. Opening and closing the database each time you need to preform a transaction is not a bad idea either. Unless your application is constantly writing data, you shouldn't lose performance

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号