Skip to content
DeveloperMemos

How to Prepopulate an Android Room Database with Initial Data

Android, Room, Database1 min read

Prepopulating a database with data is a common requirement in many Android applications. It allows you to provide users with a preconfigured app experience or ship your app with default data that users can start with. In this article, we will explore how to prepopulate an Android Room database with initial data.

Overview of Room

Room is a powerful library provided by the Android Jetpack architecture components that makes it easy to work with a SQLite database in Android applications. It provides an abstraction layer over SQLite and offers compile-time verification of SQL queries. Room consists of three main components:

  1. Entity: Represents a table within the database.
  2. DAO (Data Access Object): Contains the methods to perform CRUD (Create, Read, Update, Delete) operations on the database.
  3. Database: Acts as the main access point to the underlying SQLite database. It manages the creation and versioning of the database.

Prepopulating Room with Data

To prepopulate a Room database with data, we can leverage the RoomDatabase.Callback class provided by Room. This class allows us to hook into different lifecycle events of the database, such as creation and opening. By overriding the onCreate() method, we can populate the database with our initial data.

Here's an example of how to prepopulate a Room database using Kotlin:

1@Database(entities = [User::class], version = 1, exportSchema = false)
2abstract class AppDatabase : RoomDatabase() {
3 // Define your DAO interfaces here
4
5 companion object {
6 @Volatile
7 private var INSTANCE: AppDatabase? = null
8
9 fun getDatabase(context: Context): AppDatabase {
10 return INSTANCE ?: synchronized(this) {
11 val instance = Room.databaseBuilder(
12 context.applicationContext,
13 AppDatabase::class.java,
14 "app_database"
15 )
16 .addCallback(object : RoomDatabase.Callback() {
17 override fun onCreate(db: SupportSQLiteDatabase) {
18 super.onCreate(db)
19 // Populate the database with initial data
20 // For example:
21 val userDao = INSTANCE?.userDao()
22 userDao?.insert(User("John Doe"))
23 userDao?.insert(User("Jane Smith"))
24 }
25 })
26 .build().also { INSTANCE = it }
27 }
28 }
29 }
30}
31
32@Entity(tableName = "users")
33data class User(
34 @PrimaryKey val id: String = UUID.randomUUID().toString(),
35 val name: String
36)
37
38@Dao
39interface UserDao {
40 @Insert
41 suspend fun insert(user: User)
42
43 // Define other operations here
44}

In the above example, we have defined an AppDatabase class that extends RoomDatabase. Inside the companion object, we override the onCreate() method of RoomDatabase.Callback and populate the database with initial User entities.

Note that the onCreate() method is called only when the database is created for the first time. If the database already exists, this method won't be called. If you need to update the initial data in subsequent versions of the database, you can make use of the onOpen() method.

Closing Thoughts

Prepopulating an Android Room database with initial data allows you to provide users with a preconfigured app experience or ship your app with default data. In this article, we explored how to achieve this using the RoomDatabase.Callback class provided by Room. By overriding the onCreate() method, we can insert the initial data into the database during its creation. With this knowledge, you can now enhance your Android applications by prepopulating Room databases with data.