3

Room persistence library version 2.2.0-alpha01 has added the ability to use pre-packaged databases.

https://developer.android.com/jetpack/androidx/releases/room

Can someone provide an example for how to initialize the room database builder?

MikeT
  • 51,415
  • 16
  • 49
  • 68
syloc
  • 4,569
  • 5
  • 34
  • 49

1 Answers1

4

I'm using this :

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

    companion object {
        private const val DATABASE_NAME = "you_name"
        private const val DATABASE_DIR = "database/you_name.db" // Asset/database/you_name.db

        fun getInstance(context: Context): AppDataBase {
            return Room
                    .databaseBuilder(context, AppDataBase::class.java, DATABASE_NAME)
                    .createFromAsset(DATABASE_DIR)
                    .build()
        }
    }

    abstract fun getUsers(): UsersDao
}

for more information refer here

If you need update DB from Asset!
 1. You need level up version Database in settings Room!
 2. Add .fallbackToDestructiveMigration() method in getInstance
 3. And need level up version in you db file;
Ebrahim Karimi
  • 732
  • 8
  • 24
Evgen But
  • 159
  • 1
  • 3