Open In App

How to Prepopulate Room Database in Android?

Last Updated : 18 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Implementation

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.Entity
import 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.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
  
@Dao
interface 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.Context
import androidx.room.Database
import androidx.room.Room
import 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads