Android OS provides us inbuilt SQLite core library for store raw data in local database. It handle CRUD (Create, Read, Update and Delete) operations that require for a database. SQLite maintains an effective database management system.
But, Google has introduced Room persistence library which allows fluent database access while harnessing the full power of SQLite. Room is a new way to create a database in your android apps, it is much similar OrmLite. Let's start with a most obvious question about Room.
Why should I use another library to manage my database when I am already well equipped with SQLite usage?
Let's review problem with SQLite usage.
1. There is no compile-time verification of raw SQL queries.
Example:- If you write a SQL query with a wrong column name that does not exist in real database then SQLite will give exception during run time and you can not capture this issue during compile time.
2. If you want change or update the database with the help of SQL queries. This process may be time consuming and produce error.
3. We have to use lots of boilerplate code to convert between SQL queries and Java data objects.
To overcome this, Google has introduced Room Persistence Library. It has an abstraction layer for the existing SQLite APIs. All the required packages, parameters, methods, and variables are imported into an Android project by using simple following annotations.
Room have three major components that we need create and handle database quarries.
1. Database: By using database annotation you can create data holder class. Here, we have to declare entities and version of database. The annotated class should be an abstract class that extends RoomDatabase. At runtime, you can acquire an instance of it by calling Room.databaseBuilder() or Room.inMemoryDatabaseBuilder().
2. DAO: DAOs are the main component of Room. It will handle all the queries and define methods that access the database. As you can see, each query define own action.
3. Entity : This component represents a database row of table. Each field of the entity is represent a column in the database.
We have seen all the basic components and annotations of Room. Let's dive into implementation and understand it. We going to create a simple database that will store user information like: first name, last name, age.
1. Create a new project in Android Studio with empty activity and add following dependencies in module build.gradle.
2. Now, lets create an entity called User. It defines a attributes of your table and here i'm going to declare one field as primary key. It has property to auto generate values. Class is annotated with @Entity and name of the table. To make field primary key, you need to annotate a field with @PrimaryKey and property autoGenerate which assign automatic IDs. Room will create a user table with defined attributes.
3. Now create a data access object using an interface as define above. This class is annotated with @Dao annotation. Room will generate an implementation of defined methods. There are three annotations @Query, @Insert, @Delete to perform CRD operations. @Query annotation is used to perform read operation on database.
4. After that, create a database holder class that extends RoomDatabase. Here, i'm going to define user entity and database version. You can declare all the entities if you have. Class is annotated with @Database annotation. It is good practice to use singleton approach for the database, so you need to create an static method which will return instance of AppDatabase.
You are done!. We have setup database with a data holder user class that represent a row and an interface that we will use for perform CURD operations.
Now, you can add users in the user table of the database. You must perform queries on worker thread, otherwise your application will crash.
At the end, I found various thing about Room.
1. I found it easier than using SQLite directly.
2. It makes you verify your database code during compile time.
3. It makes your code more modular and readable as you divide your database code in three components like Entity , DAO and Database.
If still you have any doubt, please feel free to clarify that in the comment section below. Your Comments and suggestions are welcome.
But, Google has introduced Room persistence library which allows fluent database access while harnessing the full power of SQLite. Room is a new way to create a database in your android apps, it is much similar OrmLite. Let's start with a most obvious question about Room.
Why should I use another library to manage my database when I am already well equipped with SQLite usage?
Let's review problem with SQLite usage.
1. There is no compile-time verification of raw SQL queries.
Example:- If you write a SQL query with a wrong column name that does not exist in real database then SQLite will give exception during run time and you can not capture this issue during compile time.
2. If you want change or update the database with the help of SQL queries. This process may be time consuming and produce error.
3. We have to use lots of boilerplate code to convert between SQL queries and Java data objects.
To overcome this, Google has introduced Room Persistence Library. It has an abstraction layer for the existing SQLite APIs. All the required packages, parameters, methods, and variables are imported into an Android project by using simple following annotations.
Room have three major components that we need create and handle database quarries.
1. Database: By using database annotation you can create data holder class. Here, we have to declare entities and version of database. The annotated class should be an abstract class that extends RoomDatabase. At runtime, you can acquire an instance of it by calling Room.databaseBuilder() or Room.inMemoryDatabaseBuilder().
2. DAO: DAOs are the main component of Room. It will handle all the queries and define methods that access the database. As you can see, each query define own action.
3. Entity : This component represents a database row of table. Each field of the entity is represent a column in the database.
We have seen all the basic components and annotations of Room. Let's dive into implementation and understand it. We going to create a simple database that will store user information like: first name, last name, age.
1. Create a new project in Android Studio with empty activity and add following dependencies in module build.gradle.
2. Now, lets create an entity called User. It defines a attributes of your table and here i'm going to declare one field as primary key. It has property to auto generate values. Class is annotated with @Entity and name of the table. To make field primary key, you need to annotate a field with @PrimaryKey and property autoGenerate which assign automatic IDs. Room will create a user table with defined attributes.
3. Now create a data access object using an interface as define above. This class is annotated with @Dao annotation. Room will generate an implementation of defined methods. There are three annotations @Query, @Insert, @Delete to perform CRD operations. @Query annotation is used to perform read operation on database.
4. After that, create a database holder class that extends RoomDatabase. Here, i'm going to define user entity and database version. You can declare all the entities if you have. Class is annotated with @Database annotation. It is good practice to use singleton approach for the database, so you need to create an static method which will return instance of AppDatabase.
You are done!. We have setup database with a data holder user class that represent a row and an interface that we will use for perform CURD operations.
Now, you can add users in the user table of the database. You must perform queries on worker thread, otherwise your application will crash.
At the end, I found various thing about Room.
1. I found it easier than using SQLite directly.
2. It makes you verify your database code during compile time.
3. It makes your code more modular and readable as you divide your database code in three components like Entity , DAO and Database.
If still you have any doubt, please feel free to clarify that in the comment section below. Your Comments and suggestions are welcome.