Room library can copy db from asset folder?

后端 未结 2 1265
無奈伤痛
無奈伤痛 2021-01-25 00:37

I have old project with Pre-loaded database in Assets folder. The project have SQLiteOpenHelper implemented for database operations. But now to update app I want to move my proj

相关标签:
2条回答
  • 2021-01-25 00:45

    Is there any method or feature available in Room library with I can use preloaded db file in app at runtime.

    Starting with Room 2.2.0, RoomDatabase.Builder supports createFromAsset() and createFromFile() for setting up a database with pre-loaded initial content. See the documentation for more.

    0 讨论(0)
  • 2021-01-25 00:50

    For future people possibly landing here like me in search of the answer - Room finally added the ability to load from assets as of 2.2.0-alpha01

    Just put the database in assets folder (it can also be in a subfolder in assets) and access it like this:

    val db = Room.databaseBuilder(applicationContext, MyDatabase::class.java, "database.db")
            .createFromAsset("/databases/database.db")
            .build()
    

    If the database is not in a subdirectory, you just put the name without any additional path.

    0 讨论(0)
提交回复
热议问题