Room persistence library introduced at Google I/O 2017 provides an abstraction layer over the SQLite database which in turn provides enhanced security, easy access, easy to setup and quick to get started with new database. All the DML (Data Manipulation Language) commands are now annotated except SELECT command which works with @Query.

Room is part of Android Architecture components, but it is a database library so we split the description into different points. It’s an abstract layer over SQLite that significantly simplifies query building. Its possibilities are amazing and oriented towards the observer pattern. Now, it’s easier to write a query using annotations.

Room is not a database it just provide an abstraction layer over the existing SQLite database which is available since from Android 1.0 but the new features that it provide is the way to store,retrieve and modify data to the database. It also offers compile time check for the table name, column name and live monitoring for database changes using LiveData.

How to Use Room :

In Room, there are 3 types of components:

Database – for creating the database holder

         @Database(entities = {Example.class}, version = 3)
public abstract class AppDatabase extends RoomDatabase {
public abstract ExampleDao exampleDao();

Entity – represents class and one row in database

public class Example{
private int id;
@ColumnInfo(name = "name")
private String name;
@ColumnInfo(name = "city")
private String city;
// ... Getters and setters

DAO – component represents an interface as a Data Access Object (DAO). Main component for defining methods which access the database

public interface ExampleDao {
@Query("SELECT * FROM example")
List getAll();
@Query("SELECT * FROM example WHERE id IN (:examplesIds)")
List loadAllByIds(int[] examplesIds);
void insert(Example example);

Room also integrates with RxJava and the new Live Data.

Difference between SQLite and Room persistence library:

  1. In case of SQLite, There is no compile time verification of raw SQLite queries. But in Room there is SQL validation at compile time.
  2. As your schema changes, you need to update the affected SQL queries manually. Room solves this problem.
  3. You need to use lots of boilerplate code to convert between SQL queries and Java data objects. But, Room maps our database objects to Java Object without boilerplate code.
  4. Room is built to work with LiveData and RxJava for data observation, while SQLite does not.

Why Room Persistent Library?

There are umpteen number of reasons to choose Room Persistent Library like time and cost savings including faster fetching of data. But other more important ones are as described below.

  1. Room Persistent library reduces the amount of Boilerplate code to convert between SQL queries and Java data objects. Which can save your time!
  2. Room database checks SQL statements at the compile time while querying for data and live monitoring for the changes using LiveData
  3. In Room database, all DML commands are annotated except SELECT command which works with @Query.
  4. Room has enhanced security, easier to access and set up and is quick to start with new database!