简体   繁体   中英

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

@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? Does Room provide a particular method, which I missed, for this purpose?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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