Room和SQLiteDatabaseCorruptException

时间:2018-12-17 14:18:03

标签: android android-room

在我的游戏机上看到一个报告:

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)

有人知道处理这些问题的好方法吗?尤其是在使用LiveData时,我还没有找到解决此问题的好方法。另外,重现此内容的好方法是什么-那么如何优雅地破坏数据库?

1 个答案:

答案 0 :(得分:0)

  

有人知道处理这些问题的好方法吗?

好吧,如果您的数据库已损坏,则它已损坏。除非您从头开始重新创建它,否则可能无法修复它。但是,如果您想在进行任何操作之前先检查它是否有效,只需参考这篇文章:Android: Check if a file is a valid SQLite database

编辑:好的,it seems it's possible to fix a SQLite database确实取决于它的损坏程度,因此您可能不会编写通用代码来修复它们。

  

还有什么是重现此事的好方法-那么我该如何腐败   数据库优雅吗?

您将不得不在有根电话上重现它,因为您将无法通过标准SQLite语句破坏数据库(特别是为了防止这种情况)。

拿一个虚拟电话(或仿真器),然后将其root,然后访问此文件:

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

您可以使用任何编辑器将其打开,然后将其弄乱(删除几行然后保存)。很有可能这会损坏文件,并且您将获得SQLiteDatabaseCorruptException