Using Room Database- Android

Room library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

When we think about saving data to a database the first thing that comes to our mind is SQLite. Although SQLite is a very powerful way to save the save it requires a lot of code that doesn’t provide real value. The architecture it follows is a little fuzzy and complex to understand. The answer to this problem is ROOM. It allows us to create and manage databases more easily without compromising security and integrity.

Benefits of using ROOM

  • Convenience annotations that minimize repetitive and error-prone boilerplate code.
  • Streamlined database migration paths.

Components of ROOM

Database Class:

Entity:

Data Access Object (DAO):

Room Library Architecture

Let’s start the implementation with the help of an example.
The first step is to create a class that represents a table in the database. All the classes with @Entity annotation represent a table and all the variables represent columns in the database as can be seen below. If one wants the id to be autogenerated then one can use the “autogenerate” property. A point to be noted, by default names of class and variables are copied as table names and columns unless defined otherwise as shown below. This means there is no need to define the table, column names if you want the same to be of class and variable name.

The second step is to DAO. In Room architecture database can be accessed through Dao’s only. This is the place where all the CRUD operations to a table are defined. As can be seen in the example shown below we are inserting user by using @Insert annotation.

Some other possible opertations can be as seen below

@Query(“SELECT * FROM user”)
fun getUsers(): List

@Update
fun user(user: UserEntity)

@Query(“SELECT * FROM user WHERE name == :name”)
fun getUserByName(name: String): List

@Delete
fun deleteUser(user: UserEntity)

The third step is to create a Database class that holds the connection to the SQLite database.
The database class is defined by annotating @Database and it requires a list of entities. For eg if there are two entity in your project named UserEntity and ProductEntity then its declaration would look something like this.=

@Database(entities = arrayOf(UserEntity::class,Product::class), version = 1)

While defining database class we also need to mention the database version

Database class contains the abstract function of each entity defined. The function returns corresponding Dao.

Let's have a look at how we are going to access this database from our activity class

Here we have created an object of userEntity and fed all the information manually in the object and used that object to insert data in the database in

That’s it for now!!
Thanks for reading and don’t forget to share with your fellow developers :)
This post was originally posted on
CodeTheraphy.com.

For more articles follow me on Medium and CodeTheraphy.com you can also connect me on LinkedIn

Android | Kotlin | Full stack developer | Blogger