Room Database | Android Jetpack - Part 2 - Androhub

Room Database | Android Jetpack – Part 2

In the first part of the Room Database article, we learned how to integrate Room Database to Android applications and perform CRUD operations.

In this article, we will learn Room Database something more in detail.

VIDEO TUTORIAL

@Ignore

If we have to ignore any column from the Entity then you can use @Ignore annotation

or you can use the ignoredColumns attribute in Entity to ignore columns.

@Embedded

If we have to add any existing Entity or POJO/Model class to Entity class then we can use @Embedded annotation.

Alter Entity

To alter any Entity in Room Database then we have to follow the below steps:

  1. Increase the Database Version.
  2. Adding the Migration Rules: By doing this you can specify which column you want to alter without dropping the tables.

    or use Fallback Destructive Migration: By doing this database will drop all tables and recreate the tables again.

Room Database Internal Working

The room will auto-create Impl class during compile time for every @Dao and it will look like below. Here if you see how it’s auto-creating queries for Create, Insert, Delete, and Update and also using Cursors to return data.

Multiple CRUD Operations

If we have to insert, delete, or update multiple data at a time then we can use the below code. The below methods are taking array[] of User but we can use List as well.

Base Dao

If we work on the bigger project then we see for all Entities we have some queries which are common everywhere. So to remove the duplication we can create a Generic BaseDao and it can be extended by other Dao’s.

You can add other common methods as well depending upon the redundancy.

Custom Query Parameterised

If we have to write the query which will take some field as input and on that basis it should return the data then we can use something like below:

Debug your Database

Let’s suppose we have to debug our database means checking that the values are properly inserting or not and what is the schema of the Database. There are two ways we can do this:

  1. Database Inspector: The Database Inspector allows you to inspect, query, and modify your app’s databases while your app is running. This is especially useful for database debugging. The Database Inspector works with plain SQLite and with libraries built on top of SQLite, such as Room.
    This functionality is only available in Android Studio 4.1+ and the app running on API Level 26+.

    Select App
    List of All Database Tables
    Browsing Tables
    Live Updates
    Opening Query Tab
    Running Query

     

  2. Database Debug Bridge: There is a very famous and popular dependency that can be used with any app if you don’t have a device with API Level 26+ or due to any other reason.
    Just add the below dependency to your Gradle file and sync it.

    Once you are done with syncing run your app and you will see the local URL in your logs. Click on the URL and it will open the browser where you can see your database and preferences.
    Note: Your device and system both should be on the same wifi network else you will get URL as 0.0.0.0.

    Debug URL in LogCat

    URL opened in Browser

If you have a rooted device or you are running your app in Emulator then you can directly access the database from your internal storage but from there you cannot modify or do any changes which can affect your app database.

You can find all the additional annotations discussed above in the following download link.

Thanks. 🙂

 

Post comment

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