How to use Room database in an Android app?

Room is an ORM (Object Relational Mapping) library provided by Android that allows you to manage local database storage with SQLite in a more structured and type-safe manner. It helps you to create, read, update, and delete data seamlessly without the complexity of managing raw SQLite queries.

Using Room Database in an Android App

Follow these steps to implement Room database in your Android application:

1. Add Dependencies

First, add the required Room dependencies in your build.gradle file:

    implementation "androidx.room:room-runtime:2.4.2"
    annotationProcessor "androidx.room:room-compiler:2.4.2" // use kapt for Kotlin
    

2. Create Entity

Create an entity class that represents a table in your database:

    @Entity(tableName = "user_table")
    public class User {
        @PrimaryKey(autoGenerate = true)
        private int id;
        private String name;
        
        // Getters and Setters
        // ...
    }
    

3. Create DAO

Define a Data Access Object (DAO) interface for database operations:

    @Dao
    public interface UserDao {
        @Insert
        void insert(User user);

        @Query("SELECT * FROM user_table")
        List getAllUsers();
        
        @Delete
        void delete(User user);
    }
    

4. Create Room Database

Build the Room database by creating an abstract class that extends RoomDatabase:

    @Database(entities = {User.class}, version = 1)
    public abstract class UserDatabase extends RoomDatabase {
        public abstract UserDao userDao();
    }
    
    // To get instance
    UserDatabase db = Room.databaseBuilder(getApplicationContext(),
            UserDatabase.class, "user_database").build();
    

5. Using the Room Database

Finally, you can perform database operations using the DAO methods. Use AsyncTask or coroutines for background operations:

    new AsyncTask() {
        @Override
        protected Void doInBackground(Void... voids) {
            User user = new User();
            user.setName("John Doe");
            db.userDao().insert(user);
            return null;
        }
    }.execute();
    

Room Database Android App SQLite ORM Local Database