Android Mini project and tutorial : create and manage SQLite internal databases with Room persistence library with Kotlin
In this tutorial we will learn how to work with the Android Room library to create and manipulate internal SQLite databases in our android applications.
Previously, we were able to create our database and our tables and manage our data thanks to SQLiteOpenHelper and SQLiteDatabase classes. This meant entering a large amount of standard code to create and manipulate even a very small database. In addition, no compile-time verification of SQL queries was possible. And if the database schema is modified, we had to update the affected SQL queries.
But now with Jetpack libraries we have become able to create and manipulate our internal databases in more flexible ways thanks to Room library.
The Room library provides an abstraction layer on SQLite, which allows more robust access to the database while offering all the power of SQLite. Room is an ORM, Object Relational Mapping library. In other words, Room will map our database objects to Java objects and provide an abstraction layer on SQLite to allow smooth database access while harnessing the full power of SQLite.
In this article, we'll be creating a mini contact manager project while discussing the basics of using the Room library and how it can improve your workflow as a developer.
The Room library is made up of 3 main components:
Entity:
Usually this is a Pojo (Plain Old Java Object) class. The entity represents a table in the database and must be annotated with @Entity. Each entity consists of at least one field to define a primary key. Specify the name of the table if you want it to be different from the name of the class by adding to the annotation : @Entity (tableName = “name_of_your_table”).
Each entity needs a primary key. The primary key can be specified by adding the @PrimaryKey annotation before the concerned variable . For other variables, if you want the name of the column in the table to be different from the name of the variable, you can add this annotation to it: @ColumnInfo (name = “name_of_your_column”) .
Note that each field (represented by a class variable) in the database must be public or have a "getter" method.
DAO (Data Access Object):
In Room, you use data access objects to access and manage your data. The DAO is the main component of Room and includes methods that provide access to your database, it is an interface that must be annotated with @Dao. DAOs are used in place of query builders and allow you to separate different components of your database, making it easy for you to test your database.
Database:
Serves as the database owner and is the primary point of access to your relational data. It should be annotated with @Database and inherits from the RoomDatabase class. It also contains and returns the Dao (database access object). Things to know here:
• This is an abstract class that must extend RoomDatabase.
• It must be annotated with @Database, and it receives a list of entities with all the classes that make up the database (all these classes must be annotated with @Entity). We also need to provide a database version.
• We need to declare an abstract method for each of the entities included in the @Database annotation. This function must return the corresponding DAO (a class annotated with @DAO).
• Finally, we declare an object to get static access to the getAppDatabase method, which gives us a singleton instance of the database. We have defined a singleton, AppDatabase, to avoid opening multiple instances of the database at the same time. getAppDatabase returns the singleton. It will create the database on its first access, using Room's database generator to create a RoomDatabase object in the context of the application from the AppDatabase class and names it "contactsManager".
So in this project we will create a database named contactsManager which contains a single table named contact (_id, name_contact, phone_contact).
In this video we will find all the source code of this project witn explanation step by step :
Commentaires
Enregistrer un commentaire
Your comment will be reviewed by moderators