The Room persistence library provides a number of benefits over using the SQLite APIs directly:
- Compile-time verification of SQL queries
- Convenience annotations that minimize repetitive and error-prone boilerplate code
- Streamlined database migration paths
If your app has a non-Room SQLite implementation, read this page to learn how to migrate to Room. If Room is the first SQLite implementation in your app, see Save data in a local database using Room for basic usage.
Migration steps
Perform the following steps to migrate your SQLite implementation to Room. If your SQLite implementation uses a large database or complex queries, you might prefer to migrate to Room gradually. For more information about an incremental migration strategy, see Incremental migration.
Update dependencies
To use Room in your app, you must include the appropriate dependencies in your
app's build.gradle file. For more information about Room dependencies, see
Setup.
Update model classes to data entities
Room uses data entities to represent the tables in the database. Each entity class represents a table and has properties that represent columns in that table. Follow these steps to update your existing model classes to be Room entities:
- Annotate the class declaration with
@Entityto indicate that it's a Room entity. You can optionally use thetableNameproperty to indicate that the resulting table should have a name different from the class name. - Annotate the primary key property with
@PrimaryKey. - If any of the columns in the resulting table should have a name that is
different from the name of the corresponding property, annotate the property
with
@ColumnInfoand set thenameproperty to the correct column name. - If the class has properties that you don't want to persist in the database,
annotate those properties with
@Ignoreto indicate that Room shouldn't create columns for them in the corresponding table. - If the class has more than one constructor, indicate which constructor Room
should use by annotating all of the other constructors with
@Ignore.
@Entity(tableName = "users") data class User( @PrimaryKey @ColumnInfo(name = "userid") val id: String, @ColumnInfo(name = "username") val userName: String?, @ColumnInfo(name = "last_update") val date: Date?, )
Create DAOs
Room uses data access objects (DAOs) to define functions that access the database. Follow the guidance in Accessing data using Room DAOs to replace your existing query functions with DAOs.
Create a database class
Implementations of Room use a database class to manage an instance of the
database. Your database class should extend RoomDatabase and reference
all the entities and DAOs you've defined.
@Database(entities = [User::class], version = 2) @ColumnTypeConverters(DateConverter::class) abstract class UsersDatabase : RoomDatabase() { abstract fun userDao(): UserDao }
Define a migration path
Because the database version number is changing, you must define a
Migration object to preserve the existing database data. If the
database schema doesn't change, this migration can be empty.
val MIGRATION_1_2 = object : Migration(1, 2) { override suspend fun migrate(connection: SQLiteConnection) { // Empty implementation, because the schema isn't changing. } }
For more information about database migration paths in Room, see Migrate your database.
Update the database instantiation
After you've defined a database class and a migration path, you can use
Room.databaseBuilder to create an instance of your database with the
migration path applied:
val db = Room.databaseBuilder<UsersDatabase>(applicationContext, "database-name") .addMigrations(MIGRATION_1_2) .build()
Test your implementation
Make sure you test your new Room implementation:
- Follow the guidance in Test migrations to test your database migration.
- Follow the guidance in Test your database to test your DAO functions.
Incremental migration
If your app uses a large, complex database, it might not be feasible to migrate your app to Room all at once. Instead, you can optionally implement the data entities and Room database as a first step and then migrate your query functions into DAOs later.
To implement an incremental migration, obtain a SupportSQLiteDatabase
compatibility wrapper using the roomDatabase.getSupportWrapper extension
function from the androidx.room3:room3-sqlite-wrapper artifact. This wrapper
lets you execute direct Android-style SQL queries on the Room-managed database
using Android SQLite APIs:
// Get SupportSQLiteDatabase wrapper val legacyDb = roomDatabase.getSupportWrapper() legacyDb.execSQL("INSERT INTO users ...")