Skip to content Skip to sidebar Skip to footer

How To Recreate Room Database From Assets?

I have preloaded database in Asset folder and it contain all data , when i Edit my database in Asset and increase version number and using fallbackToDestructiveMigration() app is w

Solution 1:

You can prepopulate it with your initial values when its created

Room.databaseBuilder(context.applicationContext,
    DataDatabase::class.java, "Sample.db")
    // prepopulate the database after onCreate was called
    .addCallback(object : Callback() {
        override fun onCreate(db: SupportSQLiteDatabase) {
            super.onCreate(db)
            // moving to a new thread
            ioThread {
                getInstance(context).dataDao()
                                    .insert(PREPOPULATE_DATA)
            }
        }
    })
    .build()

Post a Comment for "How To Recreate Room Database From Assets?"