Room Database | Android Jetpack - Part 1 - Androhub

Room Database | Android Jetpack – Part 1

In this article, We are going to learn how to implement the Room Database in Android Application.

What is Room Database?

Room is a persistence library part of Android Jetpack Components. It provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

Benefits of Room

  • Compile-time verification of SQL queries.
  • Convenience annotations that minimize repetitive and error-prone boilerplate code.
  • Streamlined database migration paths.
  • Can be integrated with LiveData, RxJava, Coroutines, Guava.

Room vs SQLite

The major difference between Room and SQLite are:

  • SQLite doesn’t verify the Queries on Compile time but in Room, there is Query validation at Compile time.
  • We have to write boilerplate code to convert between Queries and POJO/Model class but Room maps database to Java POJO/Model class without boilerplate code.
  • SQLite cannot directly work with LiveData, RxJava, Coroutines, Guava but Room can.
  • We use Cursor, Content Values in SQLite to do the operations but in Room, we don’t care about it anymore.

Components of Room

There are three major components in the Room:

  • Entity: It represents a table for the database. Any POJO/Model class annotated with @Entity will be treated as a table and added to the database. The variables inside the class will be treated as columns.
    Entities Annotations:
    There are few entities annotations we use for our POJO/Model class as follows:@PrimaryKey — This annotation points to the primary key of the entity. autoGenerate — if set to true, then SQLite will be generating a unique id for the column.

    @ColumnInfo — allows specifying custom information about column

    @Ignore — any field annotated with @Ignore will not be persisted by Room.

    @Embeded — nested POJO/Models can be referenced directly in the SQL queries.

  • Dao (Database Access Object): It provides methods that your app can use to query, update, insert, and delete data in the database.
  • Database: The database class that holds the database and serves as the main access point for the underlying connection to your app’s persisted data.
Room Architecture (Credit: https://developer.android.com/)

VIDEO TUTORIAL

Example

1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill the required details. By default my activity is MainActivity.java.

2. First we have to add Room dependency to our project. Open the build.gradle at the app level and add the below dependencies to it and sync your project.

3. Once your project is synced then we have to create the UI to take user input for storing the data. Open activity_main.xml and add the below code to it. The layout contains three EditText to take input and a ListView to show the stored data.

4. Now we have to create our table i.e. Entity. For this create a new class with the name User.java and put the below code into it.

5. After creating Entity we have to create Dao to do CRUD operations. For this create a new interface called UserDao.java and put the below code into it.

6. Now we have to create our Database. Create an abstract class called MyDatabase.java and extent it with RoomDatabase.

7. We have created all components of the Room Database. No open your MainActivity.java and paste the below code to it.

In the MainActivity we are doing the following things:

  • DB Initialisation: To initialize the Room Database we have to add the following code to our class.
  • Insert Operation: To insert the data in the User table we are using the following code.
Insertion Operation
  • Read Operation: To fetch all data from the User table we are using the following code.
Read Operation
  • Delete Operation: To delete the data in the User table we are using the following code.
Delete Operation
  • Update Operation: To update the data in the User table we are using the following code.

Note: We cannot run Room queries on the main thread. So if you see the MainActivity code all the DB Opertations are inside ExeceutorsService to run the queries on the background thread. Alternatives of Executors are RxJava, Coroutines, Guava.

Update Operation

8. Now create ListAdapter.java class to list the fetched User from the database and add the below code to it.

9. Create item_list.xml for ListAdapter and add the below code to it.

10. Finally, all done. Now you can also integrate Room Database into your application.

Thanks. 🙂

 

Post comment

Your email address will not be published. Required fields are marked *