Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this lesson, you’ll learn about Room database and how to create a Room database. Room is a Google library and is part of the Android Jetpack libraries. It provides an abstraction layer over SQLite. This makes it easier to work with SQLite databases in Android. Room uses a local SQLite database to store your data. It allows you to store structured data in a local database on the device.

Now, you’ll start by exploring the key benefits of using Room database.

Room provides the following benefits:

  • Compile-time checks: Room validates your SQLite statements at compile time. This helps you to catch errors early in the development process.
  • Less boilerplate code: Room uses annotation processors to generate code at compile time for you. Some of the existing annotation processors for Room include:   - Kapt: This is the Kotlin annotation processor. It’s used to generate code for Kotlin classes.   - KSP: This is the Kotlin Symbol Processing. It’s a new annotation processor for Kotlin that’s faster than Kapt.
  • Migration support: Room provides rich migration support. It handles changes in your database schema without losing data.
  • Seamless Integration: Room supports LiveData and Kotlin Flow. This makes it easy to observe database changes and update the UI reactively. Room also supports Kotlin Coroutines and RxJava. With this support, Room makes it easier to perform asynchronous database operations.

Now that you have an overview of the key benefits of using Room database, you’ll learn about the main components of a Room database.

Room consists of the following main components:

  • Database: A database is the main access point for the underlying SQLite database. It’s an abstract class that extends the RoomDatabase class. Inside the database class, you define the entities, the Data Access Objects (DAOs), and any other database configurations.

  • Entity: An entity represents a table in a database. You define an entity as a Kotlin data class and annotate it with the @Entity annotation.

  • DAO (Data Access Object): A DAO is an interface used to define the database interactions using annotated functions. You place all Create, Read, Update, and Delete (CRUD) operations in your DAO. You annotate the DAO with the @Dao annotation.

Below is a picture of how the Room database components work together:

AppDatabase Rest of your app Data Access Objects Entities Get DAO Get entities Save changes to db Get or set field values

From the diagram, the database class provides your app with instances of your DAOs. The app can, in turn, use the DAOs to either insert, update, delete, or query the database.

Next, you’ll learn how to create a Room database.

Below is an example of how you create a Room database:

@Database(entities = [NoteEntity::class], version = 1)
abstract class DevScribeDatabase: RoomDatabase() {
  abstract fun notesDao(): NotesDao
}

In the above example, the DevScribeDatabase is an abstract class. It extends the RoomDatabase class and represents your database. You use the @Database annotation to create a database, and define your entities and your database version.

The entities parameter is used to define your entities. You use the version parameter to define the database version. The version number is used by Room to provide migration support. You have an abstract function notesDao() that returns the NotesDao that can be used to perform database operations.

Now that you’ve created your database, you need to initialize it. You’ll learn how to do that in the next section.

After defining your database, you need to create an instance of the database. You create an instance of the database using the Room.databaseBuilder() method as shown below:

Room.databaseBuilder(androidContext(), DevScribeDatabase::class.java, "dev_scribe_db")
  .build()

In the above example, you use the Room.databaseBuilder() function to create an instance of the DevScribeDatabase class. The function takes three parameters:

  • Context: Context is used to access the filesystem for storing the database.
  • Your database class: This is the class reference of the abstract class that extends RoomDatabase. It defines the database configuration and serves as the main access point to the underlying connection to the app’s persisted data.
  • Database name: This string specifies the name of the database file.

The build() function builds your database instance. With this instance, you can now access the DAO and perform database operations as shown below:

val notesDao = devScribeDatabase.notesDao()
val notes = notesDao.getAllNotes()

In the above code, you get an instance of the DAO using the notesDao() function. You then use the DAO to perform database operations, such as getting all notes from the database.

As best practice, create a single instance of your database and use it throughout your app. This is because each RoomDatabase instance is fairly expensive. You don’t want to create multiple instances of the database.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo