DB
@Database(entities = {DBVersion.class, CafeteriaData.class, SearchHistory.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase {
public abstract DBVersionDao getDBVersionDao();
public abstract CafeteriaDataDao getCafeteriaDataDao();
public abstract SearchHistoryDao getSearchHistoryDao();
private static volatile AppDatabase INSTANCE;
public static AppDatabase getInstance(Context context) {
if (null == INSTANCE) {
synchronized (AppDatabase.class) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, "word_database")
.build();
}
}
return INSTANCE;
}
}
Dao
@Dao
public interface DBVersionDao {
@Insert
public void insert(DBVersion version);
@Query("SELECT * FROM version_table")
public DBVersion get();
@Update
public int update(DBVersion version);
@Query("DELETE FROM version_table")
public void deleteAll();
}
Entity
@Entity(tableName = "version_table")
public class DBVersion {
@PrimaryKey(autoGenerate = true)
long id;
int version;
public DBVersion(int version) {
this.version = version;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
implementation 'com.android.support:support-v4:26.1.0'
annotationProcessor "android.arch.persistence.room:compiler:$room_version"
DBVersion dbVersion = dbVersionDao.get();
dbVersionDao.insert(new DBVersion(version));
dbVersion.setVersion(version);
dbVersionDao.update(dbVersion);
'Android' 카테고리의 다른 글
DialogFragment Fullscreen (0) | 2019.07.03 |
---|---|
RxJava 소스 모음 (0) | 2019.06.15 |
showToast : Toast Custom (0) | 2019.04.19 |
도로명 주소 입력 받기 ( WebView + JS 연결) (0) | 2019.04.19 |
RecyclerView SwipeToDelete (0) | 2019.04.19 |