Room Database: An Architecture Component

What is Architecture Components?

With the popular demand, The Android Team has written an opinionated guide to architecture android applications and developed a set of architecture components.

Architecture Components are a growing set of libraries there will be more of them coming out and they are meant for creating an Android Application in a better way.

The whole point of these libraries is to simplify things that might have been a small challenge with android mobile application development.

List of Architecture Components:

Room:

The room is a robust SQL object mapping library.

Lifecycle Components:

Lifecycle components like Live Data, Lifecycle Observers, View Model and Lifecycle owners helps you to handle your app’s life cycle.

Paging Library:

Paging library is a small library to help you to load data from server gradually.

Here, We’ll focus on Room Database.

What is Room Database?

  • The room is persistence library that provides an abstraction layer over SQLite.
  • So basically by using Room database, your SQLite code becomes much easier by implementing annotations. So now you don’t need to write a lot of code to save or store your data locally.
  • Room database library reached 1.0 stable, so you can use this in an android application without any fear.

Why Room Database?

  • Working directly with SQLite database in Android has several drawbacks like,
  • You have to write a lot of boilerplate code.
  • You have to implement object mapping for every single query you wrote.
  • A database migration is too difficult in SQLite.
  • Hard to access database.
  • If you are not careful, you can easily do the long-running operation or task on the main thread.

Advantages of Room Database

  • We just have to write a Little boilerplate code.
  • All Entity and Query will be checked at compile time so no worries to check tables and queries that might crash the application.
  • Room not only provides query error It also takes care of missing table error.
  • You can fully integrate Room Database with other Architecture Components like Live Data.

Let’s get started with Room Database

Update the dependencies

Room Database libraries are available via Google’s maven repositories, So you need to add it to the list of repositories in a project level build.gradle file.

In your app/build.gradle file, you need to add dependencies for room database.

Create Table with Room

@Entity will define a table, write table name in ‘tableName’
(Ignore if your class name is same as your table name)

@PrimaryKey will define your column as a primary key. Add ‘autoGenerate’ as a true to auto-generate value in that particular column.

@ColumnInfo is used to define that field as a table column. Write the name of your table filed in ‘name’ (Ignore if the variable name and table field name are same)

Create DAO (Database Access Object)to handle all queries

Read query

@Dao Contains all the methods which are used to access the database

@Query Here you can write all the Custom SQL Queries

Insert Query

@Insert annotation will automatically insert data in the table

Insert Query for list of data

@Insert If you want to add more than one employee than you can also do it by this annotation.(If you pass single record it will insert single If you pass the list of record it will insert all the records)

Update Query

@Update annotation used to update any of the data stored in the table

Delete Query

@Delete annotation will delete the employee data

Generate Database class to handle tables and queries

@Database annotation will create your database. In Entities, you have to specify all the entities(Tables) you have created.

Build database in your activity

Here you have to specify the name of Database and build the database after that you can access all Database query by object.

Retrieve all data employee

By this, you can get all the employee detail as a list.

Insert Employee Data

Here you have to specify employee details to insert into a table.

Update Employee Data

You can update the employee details based on unique ID.

Delete Employee Data

Delete employee data from table based on unique ID.

room-database-image1

room-database-image2

mm

Hardik Lakhani Android Developer

I am Hardik, currently working as Android Developer in Yudiz Solutions Pvt. Ltd - a leading Mobile Apps development company. As per the trend, I’m totally interested in learning about latest technology that's why I chose to be a developer as a career. That’s all about me!

Comments are closed.

Top