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
@Databaseshould 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
Databaseby callingRoom.databaseBuilder()orRoom.inMemoryDatabaseBuilder(). - Be an abstract class that extends
-
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 following code snippet contains a sample database configuration with one entity and one DAO:
User.java
@Entity
public class User {
@PrimaryKey
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.
}
UserDao.java
@Dao
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);
@Insert
void insertAll(User... users);
@Delete
void delete(User user);
}
AppDatabase.java
@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();


