How to Prepopulate Room Database in Android?
Last Updated : 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.
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.
Similar Reads
How to Perform CRUD Operations in Room Database in Android? Data from the app can be saved on users' devices in different ways. We can store data in the user's device in SQLite tables, shared preferences, and many more ways. In this article, we will take a look at saving data, reading, updating, and deleting data in Room Database on Android. We will perform
15+ min read
How to Read Data from Realm Database in Android? In the previous article, we have seen adding data to the realm database in Android. In this article, we will take a look at reading this data from our Realm Database in the Android app. What we are going to build in this article? In this article, we will be simply adding a Button to open a new act
8 min read
How to pre populate database in Android using SQLite Database Introduction : Often, there is a need to initiate an Android app with an already existing database. This is called prepopulating a database. In this article, we will see how to pre-populate database in Android using SQLite Database. The database used in this example can be downloaded as Demo Databas
7 min read
How to Read Data from SQLite Database in Android? In the 1st part of our SQLite database, we have seen How to Create and Add Data to SQLite Database in Android. In that article, we have added data to our SQLite Database. In this article, we will read all this data from the SQLite database and display this data in RecyclerView. What we are going to
12 min read
How to Update Data to SQLite Database in Android? We have seen How to Create and Add Data to SQLite Database in Android as well as How to Read Data from SQLite Database in Android. We have performed different SQL queries for reading and writing our data to SQLite database. In this article, we will take a look at updating data to SQLite database in
10 min read