Search code examples
androidandroid-studiokotlinandroid-sqliteandroid-room

How to attach filled Sqlite DB to App with Kotlin and android studio


I have an Offline application that uses Sqlite and Room. Also I have filled the DB and want to attach it to my application in Android Studio.

My question is: How? Can I do that? if yes, is that acceptable or I have to connect to internet and use Retrofit at least for one time and get data from Server?


Solution

  • Yes, it is possible: https://developer.android.com/training/data-storage/room/prepopulate

    To prepopulate a Room database from a prepackaged database file that is located anywhere in your app's assets/ directory, call the createFromAsset() method from your RoomDatabase.Builder object before calling build():

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

    The createFromAsset() method accepts a string argument that contains a relative path from the assets/ directory to the prepackaged database file.