How can I save data in a local database using Room?

In today’s digital world, data is the new oil. Managing this data efficiently and effectively is crucial for any application. In this context, let’s explore how to save data locally using Room, a popular database library for Android.

Table of Contents

  1. Introduction to Room Database
  2. Setting Up Room in Your Project
  3. Defining the Data Model
  4. Creating the DAO (Data Access Object)
  5. Inserting Data with Room
  6. Reading and Updating Data
  7. Deleting Data
  8. Summary

1. Introduction to Room Database
Room is an easy-to-use SQLite database library developed by Google. It provides a simple, clean abstraction layer over raw SQLite, making it easier to handle database operations in Android applications. Room supports all the common CRUD (Create, Read, Update, Delete) operations and offers advanced features like transaction support, live data, etc.

2. Setting Up Room in Your Project
To set up Room in your project, add the following dependency to your build.gradle file:

implementation 'androidx.room:room-runtime:2.3.0'
kapt 'androidx.room:room-compiler:2.3.0'

Don’t forget to add the KAPT processor to your buildTypes.

3. Defining the Data Model
Create a data class representing the entity you wish to save, annotated with @Entity:

@Entity(tableName  "user")
data class User(
    @PrimaryKey val id: Int,
<h2>    val name: String,</h2>
<h2>    val age: Int</h2>
)

4. Creating the DAO (Data Access Object)

Create an interface defining your database operations using `

@Dao

`:

<h2>@Dao</h2>
interface UserDao {
    @Insert(onConflict  OnConflictStrategy.<h2>REPLACE)</h2>
<h2>    suspend fun insertUser(user: User):</h2> <h2>Long</h2>

    // Other CRUD methods go here...
}

5. Inserting Data with Room

To insert data, use the `insertUser()` method defined earlier:

@HiltViewScoped
class UserRepository @Inject constructor(private val userDao: UserDao) {
    suspend fun saveUser(user: User): <h2>Long</h2> {
        return userDao.insertUser(user)
    }
}

// Usage
val repository  getInstance(UserRepository::class.java)
val userId  repository.saveUser(User(1, "John Doe", 30))

6. Reading and Updating Data

Reading or updating data is as simple as querying the DAO:

@HiltViewScoped
class UserRepository @Inject constructor(private val userDao: UserDao) {
<h2>    suspend fun getUserById(id:</h2> <h2>Long</h2>): User? {
        return userDao.getUserById(id)
    }

<h2>    suspend fun updateUser(user: User): Int {</h2>
        return userDao.updateUser(user)
    }
}

7. Deleting Data

To delete data, use the `delete()` method:

@HiltViewScoped
class UserRepository @Inject constructor(private val userDao: UserDao) {
<h2>    suspend fun deleteUserById(id:</h2> <h2>Long</h2>): Int {
        return userDao.deleteUserById(id)
    }
}

8. Summary
With just a few lines of code, you’ve managed to set up and use Room in your Android application to save data locally. It offers an easy-to-use, intuitive API for performing common database operations with minimal effort. Use this knowledge to create efficient and effective data management solutions tailored to your needs.