📍 androidx.room 이란?
안드로이드 앱 개발할 때 SQLite를 활용해 데이터베이스에 엑세스할 수 있도록 만들어주는 API
(A Database Object Mapping library that makes it easy to access database on Android applications)
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class History(
@PrimaryKey val uid: Int?,
@ColumnInfo(name = "expression") val expression: String?,
@ColumnInfo(name = "result") val result: String?
)
@Entity(tableName = "student_data_table")
data class Student(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "student_id")
var id: Int,
@ColumnInfo(name = "student_name")
var name: String,
@ColumnInfo(name = "student_email")
var email: String
)
@Dao
interface HistoryDao {
@Query("SELECT * FROM history")
fun getAll(): List<History>
// 이렇게 하나씩 insert 할 수 있음
@Insert
fun insertHistory(history: History)
@Query("DELETE FROM history")
fun deleteAll()
// 이렇게 하나씩 delete 할 수 있음
@Delete
fun delete(history: History)
// 이렇게 조건을 추가할 수 있음
@Query("SELECT * FROM history WHERE result LIKE :result")
fun findByResult(result: String): List<History>
@Query("SELECT * FROM history WHERE result LIKE :result LIMIT 1")
fun findOneByResult(result: String): History
}
@Database(entities = [History::class], version = 1)
abstract class AppDatabase: RoomDatabase() {
abstract fun historyDao(): HistoryDao
}
[공식 문서 참고]
1. Room 설명
2. androidx.room 설명