Welcome to the Kotlin Room Database tutorial! In this lesson, we'll dive into the world of persisting data in Kotlin applications using the Room library. By the end of this tutorial, you'll be able to build a fully functional database for your Android apps 📝.
Room is an easy-to-use SQL database library for Android and Kotlin. It allows you to define a database, create and manipulate tables, and perform queries in a clean, type-safe manner. Room abstracts away the complexities of SQL and handles many common database operations for you.
First, let's add the Room library to our project. In the build.gradle file for the app module, add the following line:
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"Don't forget to apply the Room plugin at the top of the file:
apply plugin: 'kotlin-kapt'To define our database and its tables, we'll create a class that extends RoomDatabase. Inside this class, we'll define an @Dao (Data Access Object) interface, which contains our methods for querying and manipulating data.
import androidx.room.Database
import androidx.room.RoomDatabase
@Database(entities = [YourEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun yourDao(): YourDao
}
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
@Dao
interface YourDao {
@Query("SELECT * FROM your_table")
fun getAll(): List<YourEntity>
@Insert
fun insert(yourEntity: YourEntity)
}
data class YourEntity(val id: Int, val name: String, val age: Int)In the example above, we created a simple database with a single table called your_table, and a corresponding DAO named YourDao. The YourEntity class defines the structure of the data that we'll be storing in the database.
To use the database in our app, we'll create a singleton instance of the AppDatabase class and inject it into the activities or fragments where we need it.
import android.content.Context
object AppDatabaseInstance {
private var INSTANCE: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
if (INSTANCE == null) {
synchronized(AppDatabase::class) {
INSTANCE = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"your_database_name"
).build()
}
}
return INSTANCE!!
}
}Now that we have our database set up, we can start using it to save and retrieve data. Here's an example of how to insert, query, and update data using our YourDao instance.
val yourDao = AppDatabaseInstance.getInstance(this).yourDao()
// Insert a new item
yourDao.insert(YourEntity(0, "John Doe", 25))
// Get all items
val items = yourDao.getAll()
// Update an item
yourDao.update(YourEntity(1, "Jane Doe", 26))When your app is no longer needed, remember to close the database to free up resources.
AppDatabaseInstance.getInstance(this).close()Which line in the `build.gradle` file should be added to include Room in the project?
This tutorial should give you a solid foundation for working with the Room database in Kotlin. Happy coding! 🚀