Save data in a local database using Room

Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

Apps that handle non-trivial amounts of structured data can benefit greatly from persisting that data locally. The most common use case is to cache relevant pieces of data. That way, when the device cannot access the network, the user can still browse that content while they are offline. Any user-initiated content changes are then synced to the server after the device is back online.

Because Room takes care of these concerns for you, we highly recommend using Room instead of SQLite. However, if you prefer to use SQLite APIs directly, read Save Data Using SQLite.

There are 3 major components in Room:

  • Database: Contains the database holder and serves as the main access point for the underlying connection to your app's persisted, relational data.

    The class that's annotated with @Database should satisfy the following conditions:

    • Be an abstract class that extends RoomDatabase.
    • Include the list of entities associated with the database within the annotation.
    • Contain an abstract method that has 0 arguments and returns the class that is annotated with @Dao.

    At runtime, you can acquire an instance of Database by calling Room.databaseBuilder() or Room.inMemoryDatabaseBuilder().

  • Entity: Represents a table within the database.

  • DAO: Contains the methods used for accessing the database.

These components, along with their relationships with the rest of the app, appear in Figure 1:

The app uses the Room database to get the data access
  objects, or DAOs, associated with that database. The app then uses each DAO to
  get entities from the database and save any changes to those entities back to
  the database. Finally, the app uses an entity to get and set values that
  correspond to table columns within the database.
Figure 1. Room architecture diagram

The following code snippet contains a sample database configuration with one entity and one DAO:

public class User {
    private int uid;

    @ColumnInfo(name = "first_name")
    private String firstName;

    @ColumnInfo(name = "last_name")
    private String lastName;

    // Getters and setters are ignored for brevity,
    // but they're required for Room to work.

public interface UserDao {
    @Query("SELECT * FROM user")
    List<User> getAll();

    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
    List<User> loadAllByIds(int[] userIds);

    @Query("SELECT * FROM user WHERE first_name LIKE :first AND "
           + "last_name LIKE :last LIMIT 1")
    User findByName(String first, String last);

    void insertAll(User... users);

    void delete(User user);

@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    public abstract UserDao userDao();

After creating the files above, you get an instance of the created database using the following code:

AppDatabase db = Room.databaseBuilder(getApplicationContext(),
        AppDatabase.class, "database-name").build();

For a hands-on experience with Room, try the Android Room with a View and Android Persistence codelabs. To browse Room code samples, see the Android Architecture Components samples.