How to use Room Persistence Library with pre-populated database?

后端 未结 8 1069
别那么骄傲
别那么骄傲 2020-11-28 02:49

I\'d like to use Room with a pre-populated database, but I can\'t understand how to tell Room where to find my database.

I\'ve now put it in src/main/assets/da

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 03:14

    Room now supports Prepopulated Databases just prepare your database by using any programs like SQLite Browser or any. Then put it in Assets Folder probably in a subfolder called database then call:

    Room.databaseBuilder(appContext, AppDatabase.class, "Sample.db")
    .createFromAsset("database/myapp.db")
    .build()
    

    If you did not provide your database as an Asset but you downloaded it or it is in File System then then the method is:

    Room.databaseBuilder(appContext, AppDatabase.class, "Sample.db")
    .createFromFile(File("mypath"))
    .build()
    

    For more description or database migrations about this Feature you can check the Documentation Training.

提交回复
热议问题