Use Android Room Database Instead SQLite

Ali Talha Çoban
4 min readMar 9, 2022
src

In some of our applications we make, we need to keep data. Sometimes we keep that data in the device’s memory, and we keep some of it online. In this article I’m going to talk about the Room Database, one of Android Jetpack’s most popular libraries to keep data in device’s memory.

src

What is Room DB ?

Room Database one of the most convenient libraries come with JetPack. It provides an abstraction layer over SQLite which allows for more robust database access while still providing the full power of SQLite. This library makes it easier to work with SQLite Database objects and also helps us get rid of the code load.

So, why do we should to use Room?

Why use it ?

Room provides the following benefits:

  • Compile-time verification of SQL queries.
  • Convenience annotations that minimize repetitive and error-prone boilerplate code.
  • Streamlined database migration paths.

Because of these considerations, we (Google) highly recommend that you use Room instead of using the SQLite APIs directly.

Components

There are three components of Room which are;

  • Entity → It represents the tables within our app’s database. It is used with @Entity annotation in model class. If we think Room is a database,then the model class is table and variables of the class are columns.
  • Database → It is an abstract class where we define all our entities.
  • DAO → Stands for Data Access Object. It is an interface that includes methods that provide access to the database and perform operations within the database.

How to use it ?

I’ll show you how to use it through an example app for better understanding RoomDB. It will be a simple Film Library app.We’ll be able to use add,delete and update operations in this example.

Implementations

For Kotlin (I’ll use Kotlin);

implementation("androidx.room:room-runtime:2.4.2")
kapt("androidx.room:room-compiler:2.4.2")

Also we need to add the following

apply plugin: 'kotlin-kapt'

For Java ;

implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
androidTestImplementation 'android.arch.persistence.room:testing:1.1.1'

In addition, so that we’ll use the ViewBinding, we need to activate it in build.gradle like below ;

android {
...
buildFeatures {
viewBinding true
}
}

Entity

For each table we’ll use , we need to create an entity. In this app we create an entity class named Film

If we want to increase the primary key id one by one, set autoGenrate = true . As you can see above, we annotated class with @Entity for table, @PrimaryKey for our table’s primary key and @ColumnInfo for our each column.

Creating Dao(Data Access Object) Interface

FilmDao interface looks like the below

Database

Write the following code inside the class named FilmDatabase

In this example we’ll use three activity which are MainActivity, AddFilmActivity and SpecificFilm apart from these we’ll need an adapter class for RecyclerView in MainActivity.

Create Adapter Class

We need an adapter class for our Recyclerview in MainActivity. Create an kotlin class named FilmAdapter. Copy the code below and paste it inside FilmAdapter class. (I’ll not explain how to write adapter class in this article,I’ll do it another article later)

FilmAdapter.kt

MainActivity

And we set MainActivity that includes films we added and an add button.

activity_main.xml

MainAcitivity.kt

You can add add icon to our float action button for better looking.

res folder → drawable folder (right click)→ vector asset

Click here and type ‘’add’’ inside search bar, and select the icon.

AddFilmActivity

In this activity we’ll type the film’s informations which are name , type and director and then add it with add button.

activity_add_film.xml

AddFilmActivity.kt

SpecificActivity

This activity is come up when we click the one of the films in MainActivity. The film’s informations shown in this activity and there’ll be two button, one to delete the film, and the other to update the film.

activity_specific_film.xml

SpecificFilm.kt

In this article I tried to explain what RoomDB is and to show you how to use it in your project through an example app. I hope I could help. Stay tuned for such articles and clap the article if you like.

Contact;

--

--