Entities, DAOs und die Datenbank
Definieren Sie Ihr lokales Schema mit Room.
Entities, DAOs und die Datenbank ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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 = 0The 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.
Häufig gestellte Fragen
Ist die Lektion „Entities, DAOs und die Datenbank“ kostenlos?
Ja — der vollständige Text von „Entities, DAOs und die Datenbank“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Entities, DAOs und die Datenbank“?
Definieren Sie Ihr lokales Schema mit Room. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Entities, DAOs und die Datenbank“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Entities, DAOs und die Datenbank
- Reaktive Abfragen mit Flow-Rückgabe
- Das Repository-Pattern
- Offline-First-Caching-Strategie