0Pricing
Jetpack Compose Academy · Lezione

Entità, DAO e database

Definisca lo schema locale con Room.

Entità, DAO e database è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Entità, DAO e database» è gratuita?

Sì — il testo completo di «Entità, DAO e database» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.

Cosa imparerò in «Entità, DAO e database»?

Definisca lo schema locale con Room. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Jetpack Compose Academy?

Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Entità, DAO e database»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?

Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Entità, DAO e database
  2. Query reattive che restituiscono Flow
  3. Il pattern Repository
  4. Strategia di caching offline-first
← Torna a Jetpack Compose Academy