Entities, DAOs & Compile-Time-Verified Queries
An Abstraction Over SQLite, Not a Replacement
Room is an Android Jetpack library over SQLite — still the same on-disk storage engine, but with a developer-facing API, annotations, and generated code replacing raw SQL string manipulation and manual Cursor parsing. Its compile-time query validation catches a typo'd column name as a build error rather than a runtime crash.
Entities, DAOs & the Database Class
@Entity
data class User(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val name: String
)
@Dao
interface UserDao {
@Query("SELECT * FROM User WHERE id = :id")
fun getById(id: Long): Flow<User>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(user: User)
}
@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}An @Entity class defines a table (instance = row, property = column). A @Dao interface declares database operations as methods; Room generates the implementation. @Query methods hold raw SQL validated against the schema at compile time. The @Database class is the central hub, listing entities and exposing DAO accessors. onConflict (e.g. REPLACE) controls how a uniqueness violation on insert/update is resolved, rather than the operation failing unpredictably.
Threading & Coroutines
Room throws by default if a query runs on the main thread — blocking I/O there risks visible jank or an ANR crash. A suspend DAO function lets calling code await the result via coroutines' structured concurrency, with Room ensuring the actual query executes off the main thread.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free