INTRODUCTION TO ROOM DATABASE

Exalture Software Labs
4 min readApr 28, 2021

Room is persistence library which is part of Google’s Android jetpack library. It acts as a layer that abstracts SQLite and allows to access the database. Now, you have a question: Why should you use the Room library to manage the SQLite database. The answer to this question lies in the following comparison table:

SET UP THE ROOM
To use room in your app, add the following dependency in the build. Gradle and sync the project.

COMPONENTS OF ROOM

There are three primary components of room:
1) Entity
2) Dao
3)Database
Entity
It is a model class that is annotated with @Entity that helps to create a table in the database. This class having fields which are corresponding to the column in the table.
Entity Annotations
@PrimaryKey: It specifies the primary key of the entity. If set autogenerates to true, then generate a unique id for the columns.
@PrimaryKey(autogenerate=true)
@ColumnInfo: It specifies the information of the column of the table.
@ColumnInfo(name=”column_info”)
@Ignore: It specifies room should ignore that field
@Embedded: It specifies the field which you like to decompose into subfields.

AccountDetails.kt

Dao
It is an interface class annotated with @Dao that contains methods to insert, update, and delete methods in the database. This Dao communicates with the database using this entity.

• Insert: The @insert annotation allows to insert parameters into the appropriate table in the database.
• Update: The @update annotation allows to update specific rows in the table.
• Query: The @query annotation allows to write SQL statements.
• Delete: The @delete annotation allows to deletion of specific rows in the table.
onConflict is used to indicate what to do when a conflict occurs. Strategies available are: -
• REPLACE: It replaces the old data and continue the transaction
• IGNORE: It ignores the transaction
• ROLLBACK: It rollbacks the transaction
• FAIL: It fails the transaction
• ABORT: It aborts the transaction

DetailsInterface.kt

DetailsInterface is Dao that inserts the values into the database by calling the method insertDetails(). It updates the class by calling updateDetails() and the deletion operation performed by calling deleteDetails().

Database
It is an abstract class annotated with @database and extending the RoomDatabase. It manages the list of entities associated with the database. You can get an instance of the database by calling Room.databaseBuilder or Room.inMemoryDatabaseBuilder. When you modify the database schema, you will need to update the database version.

DetailsDatabase.kt

Here we set exportSchema as false to avoid the build warning. We define DetailDatabase as singleton to prevent the opening of multiple instances at the same time. We declare a companion object to get static access to the method getAppDatabase() which gives singleton instance of the RoomDatabase from DETAILSBASE and names it ATM.

USING THE ROOM DATABASE

Let’s see a simple example of how to use the room database

MainActivity.kt

Here we insert the details into the database by calling insertDetails(details: AccountDetails) which is defined in DetailsInterface.

Simple Tip

We can show the table by adding a dependency debugImplementation ‘com.amitshekhar.android: debug-db:1.0.4’

I hope this will helps your project. Thanks for reading.

Find complete code at https://github.com/exalturegit/creative-works/tree/initial/ATM-APP-master

Written by

Aparna .M

Visit us: https://www.exalture.com

--

--

Exalture Software Labs

Established in Palo Alto, USA, and later expanded to Kerala, India, Exalture has been coding and designing digital applications since 2013.