简体   繁体   English

如何检查 Room 数据库的完整性?

[英]How to check Room database integrity?

I have the following simple database:我有以下简单的数据库:

NameList.kt

@Entity(tableName = "name_list")
data class NameList(
        @PrimaryKey(autoGenerate = true)
        var id: Long = 0L,

        @ColumnInfo(name = "name")
        var name: String = "")

NameListDao.kt

@Dao
interface NameListDao{    
    @Insert
    fun insert(nameList: NameList)

    @Update
    fun update(nameList: NameList)

    @Query("SELECT * FROM name_list ORDER BY id DESC")
    fun getAll(): LiveData<List<NameList>>
}

NameListRepository.kt

class NameListRepository(private val nameListDao: NameListDao){
    val allNames: LiveData<List<NameList>> = nameListDao.getAll()
}

Now, when I change the table in NameList.kt by adding/removing a column such as现在,当我通过添加/删除列来更改NameList.kt的表时,例如

@Entity(tableName = "name_list")
data class NameList(
        @PrimaryKey(autoGenerate = true)
        var id: Long = 0L,

        @ColumnInfo(name = "first_name")
        var firstName: String = "",

        @ColumnInfo(name = "last_name")
        var lastName: String = "")

without deleting/changing the database on the phone, the app crashes with the following error message when the database is accessed:在不删除/更改手机上的数据库的情况下,应用程序在访问数据库时崩溃并显示以下错误消息:

E AndroidRuntime: java.lang.RuntimeException: Exception while computing database live data.
E AndroidRuntime:   at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
E AndroidRuntime:   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E AndroidRuntime:   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E AndroidRuntime:   at java.lang.Thread.run(Thread.java:764)
E AndroidRuntime: 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.
E AndroidRuntime:   at androidx.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:154)
E AndroidRuntime:   at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:135)
E AndroidRuntime:   at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:142)
E AndroidRuntime:   at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:409)
E AndroidRuntime:   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
E AndroidRuntime:   at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
E AndroidRuntime:   at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
E AndroidRuntime:   at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:476)
E AndroidRuntime:   at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:281)
E AndroidRuntime:   at androidx.room.RoomDatabase.query(RoomDatabase.java:324)
E AndroidRuntime:   at androidx.room.util.DBUtil.query(DBUtil.java:83)

I know this is due to the table being changed (when I delete the database manually and create a new one with the new entity the error is gone).我知道这是由于表被更改(当我手动删除数据库并使用新实体创建新数据库时,错误消失了)。

How can I check the integrity of the database/table (ie if they NameList database matches with the one being acessed) before accessing it?在访问它之前,如何检查数据库/表的完整性(即它们的 NameList 数据库是否与被访问的数据库匹配)? Does Room provide a particular method, which I missed, for this purpose? Room 是否为此提供了一种我错过的特定方法?

Edit: I think I now got what the version error was about.编辑:我想我现在知道版本错误是关于什么的。 But still, is it possible to check the integrity such that in case of mismatch of old and new database, I can do different things like deleting the old database or having two different databases, etc.?但是,是否可以检查完整性,以便在新旧数据库不匹配的情况下,我可以做不同的事情,例如删除旧数据库或拥有两个不同的数据库等?

You have to update Database version every time you made some changes in the structure.每次对结构进行一些更改时,都必须更新数据库版本。 As error said正如错误所说

You can simply fix this by increasing the version number.您可以通过增加版本号来简单地解决此问题。

Room takes care of you to prevent crashes in runtime. Room 会照顾您以防止运行时崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM