From the course: Android Development Essential Training: Manage Data with Kotlin

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Define an SQLite database with Room

Define an SQLite database with Room

From the course: Android Development Essential Training: Manage Data with Kotlin

Start my 1-month free trial

Define an SQLite database with Room

- After you've defined your database entities and data access objects, there's one more critical step. Defining the database itself. Do this with an abstract class that extends a class named RoomDatabase. I'll create this new class in my data package and I'll name it Monster Database. I'll set the kindest class and create the file. This class will extend Room Database and it will be abstract. And that means that you can't instantiate the class directly in your code, instead you need an instance of a sub-class. And that will actually be handled for you by the room architecture component. Now above the class declaration add an annitation of database, and pass in three arguments. Entities is a list of entity classes. You put these in as a comma-delimited list inside a pair of square brackets. For each entity class, start with the name of the class, then colon, colon class. And again, if I had more than one entity I'd list them all here. Next I'll add a version and I'll set it to one. The…

Contents