How to Prepopulate Room Database in Android? - GeeksforGeeks

18 Oct, 2021
Room is one of the Jetpack Architecture Components in Android. This provides an abstract layer over the SQLite Database to save and perform the operations on persistent data locally. This is recommended by Google over SQLite Database although the SQLite APIs are more powerful they are fairly low-level, which requires a lot of time and effort to use. But Room makes everything easy and clear to create a Database and perform the operations on it. But what is meant by Prepopulating the database? Prepopulating the database means having some pre-loaded data in the database at the first run. So, We will see the implementation for prepopulating the Room database. We will Prepopulate the database from the app assets.
We need a pre-packaged database, which we will store in the assets folder in android studio.
Step 1:
To Prepopulate a room database from a pre-packaged database that is stored in assets, We need to call the createFromAsset( ) method from Room.databaseBuilder object. Now we will create the Data Entity. It will be a data class, let’s give it the name “Quote.kt”. Refer to the below code for reference.
Kotlin
import androidx.room.Entityimport androidx.room.PrimaryKey  @Entity(tableName = "quote")data class Quote(    @PrimaryKey(autoGenerate = true)    val id: Int,    val text: String,    val author: String)
(autoGenerate = true) is used to auto-increment to id whenever a new data is added.
Step 2:
Then we need to create Dao. Dao is an interface, So no need to define methods inside it. Room takes to care for implementation of these methods. Dao is used to accessing the data object in the database. Refer to the below code for reference.
Kotlin
import androidx.lifecycle.LiveDataimport androidx.room.Daoimport androidx.room.Insertimport androidx.room.Query  @Daointerface QuoteDao {    @Insert    suspend fun insert(quote: Quote)      @Query("Select * From quote")    fun getQuote(): LiveData<List<Quote>>}
Step 3:
Now we will create the database class, it is the main access point to the application’s persisted data. It is an abstract class, It extends to RoomDatabase. Here we call the createFromAsset(“path”) method from our Room.databaseBuilder object before calling build(). Refer to the below code for reference.
Kotlin
import android.content.Contextimport androidx.room.Databaseimport androidx.room.Roomimport androidx.room.RoomDatabase  @Database(entities = [Quote::class], version = 1)abstract class QuoteDatabase : RoomDatabase() {    abstract fun quoteDao(): QuoteDao      companion object {        private var INSTANCE: QuoteDatabase? = null        fun getDatabase(context: Context): QuoteDatabase {            if (INSTANCE == null) {                synchronized(this) {                    INSTANCE =                        Room.databaseBuilder(context, QuoteDatabase::class.java, "quote_database")                            .createFromAsset("quote.db")                            .build()                }            }            return INSTANCE!!        }    }}
So, this is how we implement the Pre-populated Room persistence database.
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Retrofit with Kotlin Coroutine in Android
How to Post Data to API using Retrofit in Android?
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
How to Get Current Location in Android?
Kotlin Setters and Getters