createFromAsset and fallbackToDestructiveMigration methods of Room library - no data exists

南楼画角 提交于 2020-12-11 09:01:55

问题


I have a .db file in assets folder. My RoomDatabase class is as the following. I install the app to my device. Then I changed version = 2 in the below class, and make my prepopulated database version 2. Then i renamed one of the columns in my table so that schema is changed. Then i installed the app again. And boom! Room gives me the following error : 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.

@Database(entities = [Word::class], version = 1, exportSchema = false)
abstract class WordRoomDatabase : RoomDatabase() {

    abstract fun wordDao(): WordDao

    companion object {

        @Volatile
        private var INSTANCE: WordRoomDatabase? = null

        fun getDatabase(context: Context): WordRoomDatabase =
                INSTANCE ?: synchronized(this) {
                    INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
                }

        
        private fun buildDatabase(ctx: Context): WordRoomDatabase {
            val passphrase: ByteArray = SQLiteDatabase.getBytes("my password".toCharArray())
            val factory = SupportFactory(passphrase)

            return Room.databaseBuilder(ctx.applicationContext, WordRoomDatabase::class.java, "word_database2.db")
                    .openHelperFactory(factory)
                    .createFromAsset("word_database.db")
                    .fallbackToDestructiveMigration()
                    .build()
        }
    }
}

After this point, I completely deleted my app. Make the prepopulated database version and @Database version 1. I made allowBackup false in AndroidManifest.xml. So there is no database exist in my device. But I am still facing the same error when I install the app to device. What a nonsense error is this. How can I solve it ?


回答1:


I faced the same issue in the near past. I'm quoting from http://www.sqlite.org/lang_altertable.html

SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a colum, remove a column, or add or remove constraints from a table.

Alternatively, you can create a new table and fill the new table with data of old table.

One last and the most important point I want to say about is that if you want to change the schema of .db file exist in assets folder, then you should do that with sqlite command line. When I try to change schema by using sqlite db browser, it does not work like you said. There is a problem in sqlite db browser for changing schema.

So , if you want to change schema, do it in sqlite command line.

Increase .pragma version in both code and .db(You can increase .pragma using db browser for sqlite).

For adding removing adding data without changing schema purposes, you can safely continue to work with sqlite db browser.



来源:https://stackoverflow.com/questions/64821480/createfromasset-and-fallbacktodestructivemigration-methods-of-room-library-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!