Junior — Middle
Can you list the main parts and components of the Room library for working with databases?
sobes.tech AI
Answer from AI
Main parts of the Room library for working with databases in Android:
- Entity — classes that describe database tables. Each field of the class corresponds to a column in the table.
- DAO (Data Access Object) — interfaces with methods for data access (insertion, update, deletion, queries). Room generates the implementation of these methods.
- Database — an abstract class that inherits from RoomDatabase and combines all DAOs. It is used to create and access the database.
Example:
@Entity
data class User(
@PrimaryKey val id: Int,
val name: String
)
@Dao
interface UserDao {
@Query("SELECT * FROM User")
fun getAll(): List<User>
@Insert
fun insert(user: User)
}
@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}