Room and SQLiteDatabaseCorruptException

Viewed 1358

Just saw one report in my play console:

android.database.sqlite.SQLiteDatabaseCorruptException: 
  at android.database.sqlite.SQLiteConnection.nativePrepareStatement (Native Method)
  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement (SQLiteConnection.java:903)
  at android.database.sqlite.SQLiteConnection.executeForString (SQLiteConnection.java:648)
  at android.database.sqlite.SQLiteConnection.setJournalMode (SQLiteConnection.java:333)
  at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration (SQLiteConnection.java:298)
  at android.database.sqlite.SQLiteConnection.open (SQLiteConnection.java:217)
  at android.database.sqlite.SQLiteConnection.open (SQLiteConnection.java:195)
  at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked (SQLiteConnectionPool.java:503)
  at android.database.sqlite.SQLiteConnectionPool.open (SQLiteConnectionPool.java:204)
  at android.database.sqlite.SQLiteConnectionPool.open (SQLiteConnectionPool.java:196)
  at android.database.sqlite.SQLiteDatabase.openInner (SQLiteDatabase.java:920)
  at android.database.sqlite.SQLiteDatabase.open (SQLiteDatabase.java:908)
  at android.database.sqlite.SQLiteDatabase.openDatabase (SQLiteDatabase.java:767)
  at android.database.sqlite.SQLiteDatabase.openDatabase (SQLiteDatabase.java:757)
  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked (SQLiteOpenHelper.java:355)
  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase (SQLiteOpenHelper.java:298)
  at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase (FrameworkSQLiteOpenHelper.java:96)
  at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase (FrameworkSQLiteOpenHelper.java:54)
  at android.arch.persistence.room.RoomDatabase.query (RoomDatabase.java:233)
  at org.walleth.data.transactions.TransactionDAO_Impl$8.compute (TransactionDAO_Impl.java:1228)
  at org.walleth.data.transactions.TransactionDAO_Impl$8.compute (TransactionDAO_Impl.java:1214)
  at android.arch.lifecycle.ComputableLiveData$2.run (ComputableLiveData.java:100)
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641)
  at java.lang.Thread.run (Thread.java:764)

Anyone knows a good way to deal with these? Especially when using LiveData I do not yet see a good way to handle this problem. Also what would be a good way to reproduce this - so how do I corrupt the database elegantly?

1 Answers

Anyone knows a good way to deal with these?

Well, if your database is corrupted, it's corrupted. You probably won't be able to fix it unless you re-create it from scratch. But if you want to check if it's valid before doing anything on it, just refer to this post : Android: Check if a file is a valid SQLite database

EDIT: Okay, it seems it's possible to fix a SQLite database but it really depends on how corrupted it is, so chances are you won't write a generic code to fix them.

Also what would be a good way to reproduce this - so how do I corrupt the database elegantly?

You will have to reproduce it on a rooted phone, because you won't be able to corrupt the database via standard SQLite statements (they're especially made to prevent this).

Take a dummy phone (or emulator), then root it, and access to this file :

//data/data/<Your-Application-Package-Name>/databases/<your-database-name>

You can open it with any editor, and mess with it (delete a bunch of lines then save it). Chances are this will corrupt the file, and you'll have the SQLiteDatabaseCorruptException

Related