室:模拟器崩溃

时间:2019-08-10 14:12:55

标签: android kotlin android-room

我想添加pb的值,并且由于将pb_value包含在实体中,因此应用程序崩溃。我是新来学习室的人,我不确定将多余的项目合并到数据库中的正确方法。

E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0

    java.lang.RuntimeException: Exception while computing database live data.
        at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
        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)
     Caused by: java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
        at androidx.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:139)
        at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:119)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:142)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:409)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
        at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
        at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:452)
        at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:275)
        at androidx.room.RoomDatabase.query(RoomDatabase.java:304)
@Entity(tableName = "pb_table")
data class Pb(@PrimaryKey
              val pb: String,
              val pb_value: Double
              )
@Dao
interface PbDao {

    @Query("SELECT * from pb_table ORDER BY pb ASC")
    fun getListPbs(): LiveData<List<Pb>>


    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(pb: Pb)


    @Query("DELETE FROM pb_table")
    suspend fun deleteAll()

1 个答案:

答案 0 :(得分:1)

在我看来,您已经更改了数据库(即添加了 Pb ),但尚未编写迁移文件,也没有更改数据库版本号以显示这些数据库更改。我已经处理过几次此类崩溃,这对我来说一直就是这个意思。我一直在使用Java进行工作,因此我无法真正告诉您如何在Kotlin中进行操作(尽管,尽管我希望很快就可以开始使用),但这是Migrating Room databases从Android开发者网站。

这非常简单,尽管 Room 尚无法编写自己的迁移文件,这确实是一个痛苦。如果我没记错的话, Room 即将推出或正在开发中的某个版本将解决此问题。