Android Room (SQLite) Tutorial

In this tutorial, you'll get started with Android room. Room is an ORM or abstraction on top of SQLite database. It's part of the Architecture Components by Google. Room enables you to easily work SQLite databases in Android.

The Room persistence library acts as a layer that abstracts raw SQLite and lets you make use of robust database access while keep you close to SQLite so it doesn't get in the way when you want to access low level features of SQLite.

Requirements for Working with Android Room

To complete this tutorial, you need to have a few requirements, such as:

  • Android Studio 3.0 or later installed on your machine. You also need to be familiar with Android Studio i.e how to create, build and run/emulate a project etc.
  • An Android device or emulator if you don't wan to use a real device for testing.

Creating Android Project

Let's start by creating an Android project using Android Studio.

Open the application level build.gradle file and the dependencies for Room:

implementation  ""
annotationProcessor  ""

In the project level build.gradle file, add the google() repository:

repositories  {

After syncing gradle, you'll be ready to start creating your Android application.

Adding Room Entities

In Room, you create Java POJO classes instead of SQL tables and you decorate them with the @Entity annotation.

Go ahead and create a package for models then add the Item class:



@Entity(tableName = "items")  
public class Item {  
    @NonNull  private Long id;  
    private String name;  
    private String description;  
    private Long quantity;  

Creating a DAO Interface

DAO refers to Data Access Object which is a class that's responsible for performing CRUD operations on the Room entities.

Create a dao package and add the ItemDAO DAO interface:



public interface ItemDAO {  
  public void insert(Item... items);    
  public void update(Item... items);   
  public void delete(Item item);  

You can create a DAO by creating a Java interface and annotate it with @Dao.

Annotate the update method with @Update Annotate the insert method with @Insert Annotate the delete method with @Delete

You can insert or update a variable number of items.

Adding Custom Queries

Room allows you to execute custom SQL queries. For example, let's add a method that returns all available items in the database.

First, import the following classes:

import java.util.List;

Next, add the method:

@Query("SELECT * FROM items")  
public List<Item> getItems();

Now, let's add a method that returns an item by id:

@Query("SELECT * FROM items WHERE id = :id")  
public Item getItemById(Long id);

Creating the Room Database

We need to create an abstract class that extends RoomDatabase which will provide access to the implementations(s) of the DAO interface(s).




@Database(entities = {Item.class}, version = 1)  
public abstract class AppDatabase extends RoomDatabase {  
    public abstract ItemDAO getItemDAO();  

This is the structure of the project:

Android Room

Instantiating the Database

Now, you need to create an instance of AppDatabase by calling the databaseBuilder() method. Open the file and add the following code inside the onCreate() method:

AppDatabase database = Room.databaseBuilder(this, AppDatabase.class, "mydb")  

Calling our CRUD Methods

Finally, you can call the different methods we have created before to create, read, update and delete records from the database using Android Room:

import java.util.ArrayList;  
import java.util.List;  


ItemDAO itemDAO = database.getItemDAO();  
Item item = new Item();  
item.setDescription("Item 001");  

List<Item> items = itemDAO.getItems();    


In this tutorial, we've seen how to create a simple Android demo that uses Room for persisting data within a SQLite database.

About the Author

Techiediaries is a website dedicated to bring you tutorials for the latest web technologies

comments powered by Disqus Protection Status