Use Room database in Android

Tram Ho

In this article, we will work with Room database. Room is an ORM database based on SQLite database. Room is the recommended database for swimming in Google, allowing you to easily manipulate SQLite queries in Android.

1. Preparation

  • Android Studio 3.0 and above

2. Create Android Project

  • In app / build.gradle, add the following dependencies to use Room:

  • In Project’s build.gradle, add google () reposity:

3. Add Room Entities

  • Create the Item class as follows:

4. Create DAO Interface

5. Add queries

Room allows you to perform SQL queries like adding, editing, and deleting.

Import the following classes:

And add methods:

Query Item by id as follows:

6. Create Room Database

7. Initializing Database

Initialize the database in the MainActivity onCreate () function as follows:

Finally, we create methods to create, add, edit, delete, and update data in the database as follows:

Share the news now

Source : Viblo