开发者

Android database corrupt, but can open in SQLite Manager. Recoverable?

开发者 https://www.devze.com 2023-02-12 07:16 出处:网络
In the latest two weeks, without releasing an update to my app, I have started getting a bunch of reports with corrupted databases. Below is the stacktrace. Android cannot open the database, and neith

In the latest two weeks, without releasing an update to my app, I have started getting a bunch of reports with corrupted databases. Below is the stacktrace. Android cannot open the database, and neither could the sqlite-manager program on my computer. However, the SQLite manager-addon to firefox could open it. After running the command "compact database", the database was fixed and I could open it in android. Is there any way I could do something like this within my app? The big problem is that I cannot even attempt to open the database, because newer versions of Android will immediately delete and replace the database, as you can see in the stacktrace below. Can PRAGMA Statements somehow be executed without opening the database?

Regards,

02-22 09:55:20.245: ERROR/Database(5382): CREATE TABLE android_metadata failed
02-22 09:55:20.245: ERROR/Database(5382): Failed to setLocale() when constructing, closing the database
02-22 09:55:20.245: ERROR/Database(5382): android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
02-22 09:55:20.245: ERROR/Database(538开发者_JAVA技巧2):     at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1950)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1818)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
02-22 09:55:20.245: ERROR/Database(5382):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
02-22 09:55:20.245: ERROR/Database(5382):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)

02-22 09:55:20.245: ERROR/Database(5382): Deleting and re-creating corrupt database /mnt/sdcard/myapp/backup.sqlite
02-22 09:55:20.245: ERROR/Database(5382): android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1950)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1818)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
02-22 09:55:20.245: ERROR/Database(5382):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
02-22 09:55:20.245: ERROR/Database(5382):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
02-22 09:55:20.245: ERROR/Database(5382):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)

Edit: I manager to open the database like this:

db = SQLiteDatabase.openDatabase(database, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);

But when I run this:

String sqlQuery = "pragma integrity_check";
db.execSQL(sqlQuery);

I get this:

ERROR/AndroidRuntime(9144): Caused by: android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed: pragma integrity_check

Edit2: I have realized that vacuuming the database fixes the issue. But if I vacuum from inside my app, with exeSQL("vacuum"), it does not help. Why is this? :'(


The canonical Good Way to recover a corrupt database is to dump it out as SQL and then read it back in to a new database. It's dead easy with the sqlite tool:

sqlite in.db .dump | sqlite out.db

...but, of course, you need to do it in code, and I really don't know if .dump is available from anywhere.

I'd suggest that you possibly want to look in to how it's getting corrupted in the first place --- SQLite has always been rock solid for me, although I've only ever used it on the internal storage. Can you check the FAT filesystem to see whether it's corrupt? I've seen, e.g., digital cameras which will thoroughly mangle filesystem on cards in various obscure ways...


Android deleting the database is an issue indeed.

I just realized a solution could be to use SQLJet instead of the default Android SQLite implementation.
As its name does not suggest, SQLJet is an open source Java SQLite client, and it works on Android.


In your app does you close the database before you exit the activity? Watch the logcat and check if there are any errors or warnings when you run your app.

0

精彩评论

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