VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-perform-crud-operations-in-room-database-in-android/

⇱ How to Perform CRUD Operations in Room Database in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Perform CRUD Operations in Room Database in Android?

Last Updated : 23 Jul, 2025

Data from the app can be saved on users' devices in different ways. We can store data in the user's device in SQLite tables, shared preferences, and many more ways. In this article, we will take a look at saving data, reading, updating, and deleting data in Room Database on Android. We will perform CRUD operations using Room Database on Android. In this article, we will take a look at performing CRUD operations in Room Database in Android. 

What we are going to build in this article? 

We will be building a simple application in which we will be adding the different types of courses that are available on Geeks for Geeks. We will be storing all this data in Room Database and performing CRUD operations on these courses. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

What is Room? 

Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily. 

Components of Room

The three main components of the room are Entity, Database, and DAO

  • Entity: Entity is a modal class that is annotated with @Entity. This class is having variables that will be our columns and the class is our table.
  • Database: It is an abstract class where we will be storing all our database entries which we can call Entities.
  • DAO: The full form of DAO is a Database access object which is an interface class with the help of it we can perform different operations in our database.

Now, let's move towards the implementation of Room Database in Android. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Adding dependency for using Room in build.gradle files

Navigate to the app > Gradle Scripts > build.gradle file and add the below dependencies in the dependencies section. 

// add below dependency for using room.  

implementation 'androidx.room:room-runtime:2.2.5'

annotationProcessor 'androidx.room:room-compiler:2.2.5'

// add below dependency for using lifecycle extensions for room.  

implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.2.0'

After adding the above dependencies section. Now sync your project and we will move towards our XML file. 

Step 3: Working with the activity_main.xml file 

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

Step 4: Creating a modal class for storing our data

Navigate to the app > java > your apps package name > Right-click on it > New > Java class and name the class as CourseModal and add the below code to it. Comments are added inside the code to understand the code in more detail. 

Step 5: Creating a Dao interface for our database

Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name as Dao and select Interface. After creating an interface class and add the below code to it. Comments are added inside the code to understand the code in more detail.

Step 6: Creating a database class

Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name it as CourseDatabase and add the below code to it. Comments are added inside the code to understand the code in more detail.

Step 7: Create a new java class for our Repository

Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name it as CourseRepository and add the below code to it. Comments are added inside the code to understand the code in more detail.

Step 8: Creating a class for our Repository

Navigate to the app > java > your app's package name > Right-click on it > New > Java Class and name the class as ViewModal and add the below code to it. Comments are added inside the code to understand the code in more detail.

Step 9: Creating a layout file for each item of RecyclerView

Navigate to the app > res > layout > Right-click on it > New > layout resource file and name it as course_rv_item and add below code to it. Comments are added in the code to get to know in more detail. 

Step 10: Creating a RecyclerView Adapter class to set data for each item of RecyclerView

Navigate to the app > java > your app's package name > Right-click on it > New > Java class and name it as CourseRVAdapter and add the below code to it. Comments are added inside the code to understand the code in more detail.

Step 11: Creating a new Activity for Adding and Updating our Course

Navigate to the app > java > your app's package name > Right-click on it > New > Empty Activity and name it as NewCourseActivity and go to XML part and add below code to it. Below is the code for the activity_new_course.xml file.

Step 12: Working with the NewCourseActivity.java file

Navigate to the app > java > your app's package name > NewCourseActivity.java file and add the below code to it. Comments are added inside the code to understand the code in more detail.

Step 13: Working with the MainActivity.java file

Navigate to the app > java > your app's package name > MainActivity.java file and add the below code to it. Comments are added inside the code to understand the code in more detail.

Now run your app and see the output of the app. 

Output:

Check out the project on the below link:https://github.com/ChaitanyaMunje/GFG-Room-Database

Comment
Article Tags:

Explore