본문 바로가기
Study/Mobile

[Kotlin] ROOM Database 사용 방법

by Becoming a Hacker 2022. 1. 10.
반응형

Environment Setting

plugins{
...
}
apply plugin: 'kotlin-kapt'

dependencies {
	def room_version = "2.2.5"
    ...
    // room
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"
    // optional - RxJava support for Room
    implementation "androidx.room:room-rxjava2:$room_version"
    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"
    // Test helpers
    testImplementation "androidx.room:room-testing:$room_version"
}

 

Entity

package com.example.myapplication1

import androidx.room.*

@Entity(tableName= "vacation")
data class Vacation (
        @ColumnInfo(name="Project Name") var projectName: String,
        @ColumnInfo(name="days") var days: Int
){
        @PrimaryKey(autoGenerate = true) var id: Int = 0
}

 

Dao

package com.example.myapplication1

import androidx.room.*

@Dao
interface VacationDao {
    @Query("SELECT * FROM vacation")
    fun getAll(): List<Vacation>

    @Insert
    fun insertAll(vararg vacation: Vacation)

    @Delete
    fun delete(vacation: Vacation)
}

 

DB

package com.example.myapplication1

import androidx.room.*

@Dao
interface VacationDao {
    @Query("SELECT * FROM vacation")
    fun getAll(): List<Vacation>

    @Insert
    fun insertAll(vararg vacation: Vacation)

    @Delete
    fun delete(vacation: Vacation)
}

 

How to use ROOM in the Acitivity

btn1.setOnClickListener { // When click save button
            // SQLite에 데이터 저장 구문 추가
            // SQL SELECT or Insert
            val r = Runnable {
                val weeks: EditText = findViewById(R.id.editText2)
                var result: Int = weeks.text.toString().toInt()/3
                val vacation = Vacation(projectName.text.toString(),result)
                vacationDb?.vacationDao()?.insertAll(vacation)
            }

            val thread = Thread(r)
            thread.start()
        }

var btn2: Button = findViewById(R.id.button2)
        btn2.setOnClickListener {
            var text4: TextView = findViewById(R.id.text4)
            val r = Runnable{
                var vacationList = vacationDb?.vacationDao()?.getAll()
                runOnUiThread{
                    if (vacationList != null) {
                        text4.setText(vacationList.size.toString())
                    }
                }
            }
            val thread = Thread(r)
            thread.start()

        }

 

 

반응형

'Study > Mobile' 카테고리의 다른 글

[Kotlin] 딥링크 (DeepLink)  (0) 2022.08.07
[Kotlin] WebView 사용 방법  (0) 2022.08.07
[Android] 4대 Component  (0) 2022.08.05
iOS Application Life Cycle  (0) 2022.03.18
[Kotlin] Intellij에서 Anroid 세팅  (0) 2022.01.04

댓글