Skip to content

Latest commit

 

History

History
49 lines (38 loc) · 1.08 KB

Room.md

File metadata and controls

49 lines (38 loc) · 1.08 KB

Creating Database

Create database class and the dao like the following:

@Database(
    entities = [User::class],
    version = 1,
    exportSchema = false
)
abstract class MyDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}



/** the SimpleDao pattern provides you some basic database functional such insert and delete **/
@Dao
interface UserDao : SimpleDao<User> {

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

    @Query("SELECT * FROM User WHERE uid IN (:userIds)")
    fun loadAllByIds(
        userIds: IntArray
    ): List<User>

    @Query(
        "SELECT * FROM User WHERE first_name LIKE :first AND " +
                "last_name LIKE :last LIMIT 1"
    )
    fun findByName(
        first: String,
        last: String
    ): User

}

Ready to Use

create or bind database instance via single function :

val db by db<MyDatabase>()

See: