sggnology
하늘속에서IT
sggnology
전체 방문자
오늘
어제
  • 분류 전체보기 (83)
    • Algorithm (31)
      • Programmers (27)
      • Baekjoon (4)
    • WIKI (4)
      • VirtualBox (1)
      • Power Toys (1)
    • NodeJS (4)
      • nvm (1)
      • React (1)
      • Vue (1)
    • Dev Language (3)
      • Java (2)
      • Kotlin (1)
    • Spring Boot (17)
      • Gradle (1)
      • JPA (3)
    • DB (4)
      • MariaDB (3)
      • Redis (0)
    • Android (6)
      • Debug (3)
    • Nginx (3)
      • Debug (1)
    • Intellij (0)
    • Network (1)
    • Git (2)
      • GitHub (2)
    • Chrome Extension (0)
    • ETC (5)
      • Monitoring (2)
    • Linux (1)
      • WSL (1)
    • Visual Studio (1)
    • Side Project (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 오블완
  • JPA
  • spring boot
  • 연습문제
  • 알고리즘
  • Android Studio
  • nginx
  • mariadb
  • docker
  • java
  • 레벨3
  • 고득점KIT
  • 프로그래머스
  • 레벨2
  • kotlin
  • DB
  • 안드로이드 스튜디오
  • 고득점 Kit
  • 티스토리챌린지
  • 백준

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
sggnology

하늘속에서IT

Android :: RoomDatabase inital data(PrePopulating) 데이터베이스 초기 데이터 값 저장하기
Android

Android :: RoomDatabase inital data(PrePopulating) 데이터베이스 초기 데이터 값 저장하기

2021. 5. 20. 21:59
728x90
RoomDatabase가 처음 생성될때 초기 데이터 값을 넣는 방법이다.

 

Room은 Callback을 제공하는데, 이때 Callback내에서 db가 처음 생성될때 불리는 `onCreate`를 사용하는 것이다.

(여기서 onCreate는 모든 테이블이 생성된 뒤에 불린다고 나와있다.)

 

RoomDB Callback의 함수s


구현 설명

 

1. Entity 

public class SomeEntity{
    private String name;
    private Integer age;
}
  • SomeEntity가 있다고 가정하겠다.

2. DAO

@Dao
public interface SomeEntityDao {
    @Insert
    public void insert(SomeEntityDao);
}
  • Entity가 있으니, DAO도 있다고 가정해보자.

3. Execute part

public abstract class AppDB extends RoomDatabase {
    public abstract SomeEntityDao someEntityDao();

    private static AppDB INSTANCE;

    private static synchronized AppDB getInstance(Context ctx){
        if(INSTANCE == null){
            INSTANCE = Room.databaseBuilder(ctx, AppDB.class, "AppDB")
                    .addCallback(new Callback() {
                        @Override
                        public void onCreate(@NonNull @NotNull SupportSQLiteDatabase db) {
                            super.onCreate(db);
                            Executors.newSingleThreadExecutor().execute(new Runnable() {
                                @Override
                                public void run() {
                                    db.execSQL("INSERT INTO SomeEntity VALUES('atsky', '25')");
                                }
                            });
                        }
                    })
                    .build();
        }

        return INSTANCE;
    }
}
  • onCreate는 Room의 모든 테이블이 생성된 이후, 동작하게 됨으로 초기값이 들어가게 된다.

 


참고사이트

https://medium.com/android-news/pre-populate-room-database-6920f9acc870

 

Pre-populate Room database

→This article is out of date and it needs be upgraded ←

medium.com

 

 

틀린부분이 있다면 바로 수정할 수 있도록 하겠습니다.

읽어주셔서 감사합니다.

728x90

'Android' 카테고리의 다른 글

Android :: Notification을 사용하여 간단한 알림을 만들기(예제포함)  (0) 2021.05.17
Android :: Toolbar 사용 방법과 메뉴 커스터마이징(three dots image color, 메뉴의 위치, 예제포함)  (0) 2021.05.11
    'Android' 카테고리의 다른 글
    • Android :: Notification을 사용하여 간단한 알림을 만들기(예제포함)
    • Android :: Toolbar 사용 방법과 메뉴 커스터마이징(three dots image color, 메뉴의 위치, 예제포함)
    sggnology
    sggnology
    하늘은 파란색이니까 내 삶도 파란색이길 ㅎㅎ

    티스토리툴바