티스토리 뷰
이번 포스팅에서는 Room 을 이용한 CRUD 에 대해 알아보자.
CREATE(INSERT), READ(SELECT), UPDATE(UPDATE), DELETE(DELETE)
참고로, 지난 포스팅 내용은 하단과 같다.
- UI, DB 생성, CRUD, column 추가, DB 확인
[Android/Database] - SQLite 에서 ROOM 까지 (2) - UI 구성
[Android/Database] - SQLite 에서 ROOM 까지 (3) - SQLiteOpenHelper
[Android/Database] - SQLite 에서 ROOM 까지 (4) - SQLite 를 이용한 CRUD
[Android/Database] - SQLite 에서 ROOM 까지 (5) - SQLite 로 생성된 기존 DB 에 column 추가하기
[Android/Database] - SQLite 에서 ROOM 까지 (6) - DB 확인하기
- ROOM, ROOM 의 3구성요소 추가, Repository, ViewModel 추가, migation 설정
[Android/Database] - SQLite 에서 ROOM 까지 (7) - ROOM 이란?
[Android/Database] - SQLite 에서 ROOM 까지 (8) - ROOM 의 3 구성요소(Database, Entity, Dao) 추가하기
[Android/Database] - SQLite 에서 ROOM 까지 (10) - SQLite 에서 ROOM 으로 migration 설정하기
DB table
하단은 예제에 사용할 DB table 이다.
Field | Type | Property |
ID (StudentEntry._ID) |
INTEGER (not null) | primary key auto increment |
학년 (StudentEntry.GRADE) |
INTEGER (not null) | |
학번 (StudentEntry.NUMBER) |
INTEGER (not null) | |
이름 (StudentEntry.NAME) |
TEXT | |
나이 (StudentEntry.AGE) |
INTEGER (not null) |
Create (INSERT)
사용자 입력값을 DB 에 삽입.
private void insertData(int a_grade, int a_number, String a_strName, int a_age) { | |
PhStudentEntity studentEntity = new PhStudentEntity(); | |
studentEntity.setId(0); | |
studentEntity.setGrade(a_grade); | |
studentEntity.setNumber(a_number); | |
studentEntity.setName(a_strName); | |
studentEntity.setAge(a_age); | |
// DB 삽입 | |
mViewModel.insert(studentEntity); | |
} |
public void insert(PhStudentEntity a_studentEntity) { | |
mRepository.insert(a_studentEntity); | |
} |
public void insert(PhStudentEntity a_studentEntity) { | |
new insertAsyncTask(mStudentDao).execute(a_studentEntity); | |
} | |
private static class insertAsyncTask extends AsyncTask<PhStudentEntity, Void, Void> { | |
private PhStudentDao asyncTaskDao; | |
insertAsyncTask(PhStudentDao a_dao) { | |
asyncTaskDao = a_dao; | |
} | |
@Override | |
protected Void doInBackground(final PhStudentEntity... studentEntities) { | |
asyncTaskDao.insert(studentEntities[0]); | |
return null; | |
} | |
} |
@Dao | |
public interface PhStudentDao { | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
Long insert(PhStudentEntity a_studentEntity); | |
} |
Read (SELECT)
DB 전체를 query.
private List getAllData() { | |
PhUpdateInterface updateInterface = (a_studentEntityList) -> { | |
mItemList.addAll(a_studentEntityList); | |
mRecyclerAdapter.notifyDataSetChanged(); | |
}; | |
return mViewModel.getAllStudents(updateInterface); | |
} |
public interface PhUpdateInterface { | |
void onUpdate(List<PhStudentEntity> a_studentEntityList); | |
} |
public List<PhStudentEntity> getAllStudents(PhUpdateInterface a_updateInterface) { | |
return mRepository.getAllStudents(a_updateInterface); | |
} |
public List<PhStudentEntity> getAllStudents(PhUpdateInterface a_updateInterface) { | |
List<PhStudentEntity> students = null; | |
try { | |
students = new getAllUsersAsyncTask(mStudentDao, a_updateInterface).execute().get(); | |
} catch (ExecutionException | InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return students; | |
} | |
private static class getAllUsersAsyncTask extends AsyncTask<Void, Void, List<PhStudentEntity>> { | |
private PhStudentDao mAsyncTaskDao; | |
private PhUpdateInterface mUpdateInterface; | |
getAllUsersAsyncTask(PhStudentDao a_dao, PhUpdateInterface a_updateInterface) { | |
mAsyncTaskDao = a_dao; | |
mUpdateInterface = a_updateInterface; | |
} | |
@Override | |
protected List<PhStudentEntity> doInBackground(Void... url) { | |
return mAsyncTaskDao.getAllStudents(); | |
} | |
@Override | |
protected void onPostExecute(List<PhStudentEntity> a_studentEntityList) { | |
mUpdateInterface.onUpdate(a_studentEntityList); | |
} | |
} |
@Dao | |
public interface PhStudentDao { | |
@Query("SELECT * FROM " + StudentEntry.TABLE_NAME) | |
List<PhStudentEntity> getAllStudents(); | |
} |
Update (UPDATE)
선택한 column 을 사용자 입력값으로 DB 갱신.
DAO 에 '@Insert(onConflict = OnConflictStrategy.REPLACE)' option 을 사용해 CREATE 과정과 동일
private void updateData(int a_id, int a_grade, int a_number, String a_strName, int a_age) { | |
PhStudentEntity studentEntity = new PhStudentEntity(); | |
studentEntity.setId(a_id); | |
studentEntity.setGrade(a_grade); | |
studentEntity.setNumber(a_number); | |
studentEntity.setName(a_strName); | |
studentEntity.setAge(a_age); | |
// DB 갱신 | |
mViewModel.insert(studentEntity); | |
} |
Delete (DELETE)
선택한 column 을 DB 에서 삭제.
private void deleteData(int a_id) { | |
// DB 삭제 | |
mViewModel.deleteById(a_id); | |
} |
public void deleteById(int a_id) { | |
mRepository.deleteById(a_id); | |
} |
public void deleteById(int a_id) { | |
new deleteAsyncTask(mStudentDao).execute(a_id); | |
} | |
private static class deleteAsyncTask extends AsyncTask<Integer, Void, Void> { | |
private PhStudentDao asyncTaskDao; | |
deleteAsyncTask(PhStudentDao a_dao) { | |
asyncTaskDao = a_dao; | |
} | |
@Override | |
protected Void doInBackground(final Integer... a_id) { | |
asyncTaskDao.deleteById(a_id[0]); | |
return null; | |
} | |
} |
@Dao | |
public interface PhStudentDao { | |
@Query("DELETE FROM " + StudentEntry.TABLE_NAME + " WHERE _id = :a_id") | |
void deleteById(int a_id); | |
} |
Source code
https://github.com/parkho79/FromSQLiteToRoom_3
'Android > Database' 카테고리의 다른 글
SQLite 에서 ROOM 까지 (12) - ROOM 에 LiveData 적용하기 (0) | 2020.10.08 |
---|---|
SQLite 에서 ROOM 까지 (10) - SQLite 에서 ROOM 으로 migration 설정하기 (0) | 2020.09.21 |
SQLite 에서 ROOM 까지 (9) - Architecture Components 구성을 위한 Repository, ViewModel 추가하기 (0) | 2020.09.15 |
SQLite 에서 ROOM 까지 (8) - ROOM 의 3 구성요소(Database, Entity, Dao) 추가하기 (0) | 2020.09.09 |
SQLite 에서 ROOM 까지 (7) - ROOM 이란? (0) | 2020.09.01 |
- Total
- Today
- Yesterday
- android intent
- M2E
- 안드로이드 인텐트
- onContextItemSelected
- android task
- onCreateContextMenu
- task
- 리워드 어플
- registerForContextMenu
- 무료채굴
- BroadcastReceiver
- 앱테크 추천
- 리워드앱
- Intent
- 채굴앱
- 안드로이드 서비스
- mPANDO
- 앱테크
- Android Service
- android flag activity
- WEMIX
- p2e
- bindservice
- StartService
- 무료 채굴
- RoomDatabase
- StringUtils
- task 생성
- android activity flag
- notifyDataSetChanged
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |