Room Database (Local Storage)

June 02, 2026 1 min read

Room is Android's recommended SQLite wrapper. It gives you compile-time-checked queries and clean Kotlin APIs for local data (offline cache, notes, settings).

1) Entity = a table

@Entity(tableName = "notes")
data class Note(
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    val title: String,
    val body: String
)

2) DAO = your queries

@Dao
interface NoteDao {
    @Query("SELECT * FROM notes ORDER BY id DESC")
    fun getAll(): Flow<List<Note>>

    @Insert suspend fun insert(note: Note)
    @Delete suspend fun delete(note: Note)
}

3) The database

@Database(entities = [Note::class], version = 1)
abstract class AppDb : RoomDatabase() {
    abstract fun noteDao(): NoteDao
}
Tip: Expose lists as Flow — the UI updates automatically whenever the table changes.
Common mistake: Database calls must run off the main thread. Mark DAO functions suspend or return Flow.

Summary

Room = Entity (table) + DAO (queries) + Database. It is the standard way to persist structured data locally.