0Pricing
Jetpack Compose Academy · Lesson

Entities, DAOs & the Database

Define your local schema with Room.

Entities, DAOs & the Database is a free Jetpack Compose Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Room Is Your Local SQLite

Room is a friendly layer over SQLite. It lets you save data on the device with plain Kotlin classes instead of raw SQL strings. 💾

Three Core Pieces

Room has exactly three building blocks: an Entity for the table, a DAO for the queries, and a Database that ties them together.

An Entity Is a Table

Annotate a data class with @Entity and Room turns it into a database table, one column per property.

@Entity
data class Note(val text: String)

Every Row Needs a Key

Mark one field as the @PrimaryKey so each row is uniquely identified. Use autoGenerate to let Room assign ids for you.

@PrimaryKey(autoGenerate = true)
val id: Int = 0

The DAO Holds Your Queries

A @Dao interface declares the operations you want. You write the intent, and Room generates the implementation.

@Dao
interface NoteDao {
    // queries go here
}

Insert With One Annotation

Add a function and tag it with @Insert. Room writes the SQL that saves your entity to the table.

@Insert
suspend fun insert(note: Note)

Read With @Query

Use @Query with real SQL to fetch rows. Room checks the query against your schema at compile time.

@Query("SELECT * FROM Note")
suspend fun getAll(): List<Note>

Update and Delete Too

The same pattern covers writes: @Update and @Delete change existing rows by matching their primary key.

@Delete
suspend fun delete(note: Note)

The Database Class

Declare an abstract class with @Database, list your entities, and expose each DAO. This is the single entry point.

@Database(entities = [Note::class], version = 1)
abstract class AppDb : RoomDatabase() {
    abstract fun noteDao(): NoteDao
}

Build the Database Once

Create the instance with Room.databaseBuilder. Building it is expensive, so do it a single time and reuse it.

Room.databaseBuilder(context, AppDb::class.java, "app.db").build()

Suspend Keeps the UI Smooth

Disk access is slow, so mark DAO functions suspend. Room then runs them off the main thread and never freezes your screen.

Quick Check

Which annotation marks a Kotlin class as a Room database table?

Recap: The Three Pillars

You defined an Entity table, a DAO of suspend queries, and a Database that wires them up. Next you will make those reads reactive.

Frequently asked questions

Is the “Entities, DAOs & the Database” lesson free?

Yes — the full text of “Entities, DAOs & the Database” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “Entities, DAOs & the Database”?

Define your local schema with Room. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Entities, DAOs & the Database” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Entities, DAOs & the Database
  2. Reactive Queries Returning Flow
  3. The Repository Pattern
  4. Offline-First Caching Strategy
← Back to Jetpack Compose Academy