android:room을사용하여데이터보관
room을사용하여데이터보관
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판이전 판다음 판 | 이전 판 | ||
android:room을사용하여데이터보관 [2024/07/29 20:55] – 이거니맨 | android:room을사용하여데이터보관 [2024/12/24 20:04] (현재) – 이거니맨 | ||
---|---|---|---|
줄 30: | 줄 30: | ||
===== 뼈대 만들기 ===== | ===== 뼈대 만들기 ===== | ||
- | ==== 1. 데이터 클래스 생성 ==== | + | ==== 1. 데이터 클래스 생성 ==== |
+ | |||
+ | 데이터 클래스를 생성하여 구조화된 자료를 만들자. | ||
+ | |||
+ | 이를테면 다음과 같이 만들 수 있을 것이다. | ||
+ | |||
+ | |||
+ | <code kotlin> | ||
+ | data class Todo( | ||
+ | var id : Int = 0, | ||
+ | var title : String, | ||
+ | var createdAt : LocalDateTime | ||
+ | ) | ||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | [[android: | ||
+ | |||
+ | ==== 2. 엔티티 생성 ==== | ||
+ | |||
+ | 위 데이터 클래스에 애노테이션을 추가하면 엔티티를 만들 수 있다. | ||
+ | |||
+ | 데이터 클래스 위에 " | ||
+ | |||
+ | 그 외에 데이터베이스에서 다루는 개념들도 애노테이션으로 추가 가능하다. 이를테면 프라이머리키는 " | ||
+ | |||
+ | 다음은 그 예시다. | ||
Contact.kt를 다음과 같이 만들었다. | Contact.kt를 다음과 같이 만들었다. | ||
+ | |||
<file kotlin Contact.kt> | <file kotlin Contact.kt> | ||
줄 68: | 줄 96: | ||
다음과 같이 ContactDAO.kt | 다음과 같이 ContactDAO.kt | ||
+ | |||
+ | <file kotlin ContactDAO.kt> | ||
+ | package com.dklaw.gogo2.database | ||
+ | |||
+ | import androidx.room.Dao | ||
+ | import androidx.room.Delete | ||
+ | import androidx.room.Query | ||
+ | import androidx.room.Upsert | ||
+ | import kotlinx.coroutines.flow.Flow | ||
+ | |||
+ | @Dao | ||
+ | interface ContactDAO { | ||
+ | |||
+ | @Upsert | ||
+ | suspend fun upsertContact(contact : Contact) | ||
+ | |||
+ | @Delete | ||
+ | suspend fun deleteContact(contact: | ||
+ | |||
+ | @Query(" | ||
+ | fun getContactsOrderByFirstName() : Flow< | ||
+ | |||
+ | @Query(" | ||
+ | fun getContactsOrderByLastName() : Flow< | ||
+ | |||
+ | @Query(" | ||
+ | fun getContactsOrderByPhoneNumber() : Flow< | ||
+ | } | ||
+ | </ |
android/room을사용하여데이터보관.1722254109.txt.gz · 마지막으로 수정됨: 2024/07/29 20:55 저자 이거니맨