반응형
1. Code
A. UrlDao
@Dao
public interface UrlDao {
//입력
@Insert(onConflict = REPLACE)
void insert(UrlData urlData);
//삭제(오버로딩 삭제)
@Delete
void delete(UrlData urlData);
//전체삭제
@Delete
void reset(List<UrlData> urlData);
//값 선택
@Query("SELECT * FROM urlData where content = :sContent")
UrlData findById(String sContent);
}
B. UrlData
@Entity(tableName = "urlData")
public class UrlData implements Serializable {
// @PrimaryKey(autoGenerate = false)
// private int id;
@NonNull
@PrimaryKey
private String content;
// @ColumnInfo(name = "content")
// private String content;
@ColumnInfo(name = "image")
private String image;
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "description")
private String description;
//Id
// public void setId(int id) {this.id = id;}
// public int getId() {return id; }
//Content
public void setContent(String content){this.content = content; }
public String getContent(){return content; }
//Image
public void setImage(String image){this.image = image; }
public String getImage(){return image; }
//Title
public void setTitle(String title){ this.title = title; }
public String getTitle(){return title; }
//Description
public void setDescription(String description){this.description = description; }
public String getDescription(){return description; }
}
Primary Key에 Int넣을시 autoGenerate = true 하면 0부터 차례로 데이터 들어감
Primary Key에 String넣을시 @NonNull 넣어야함
C. UrlRoomDB
@Database(entities = {UrlData.class}, version = 1, exportSchema = false)
public abstract class UrlRoomDB extends RoomDatabase {
private static UrlRoomDB database;
private static String DATABASE_NAME = "database";
public synchronized static UrlRoomDB getInstance(Context context){
if (database == null){
database = Room.databaseBuilder(context.getApplicationContext(), UrlRoomDB.class, DATABASE_NAME)
.allowMainThreadQueries()
.fallbackToDestructiveMigration()
.build();
}
return database;
}
public abstract UrlDao urlDao();
}
2. 생성
public UrlRoomDB database;
public UrlData data;
//RoomDB 활성화
database = UrlRoomDB.getInstance(ChatViewActivity.this);
//데이터 찾아서 가져오기
data = database.urlDao().findById(event.list.get(0).trim());
//데이터 클래스에 데이터 넣기
data.setContent("스트링값");
//데이터 클래스에 데이터 빼기
data.getContent;
//RoomDB 20221107 Bell RoomDB에 데이터 넣기
InsertUrlRoomDB(response, image, title, description);
Reference
https://stickode.tistory.com/103
https://todaycode.tistory.com/39
https://developer.android.com/training/data-storage/room?hl=ko#kts
반응형
'Android > Android Java' 카테고리의 다른 글
[Android Java] ViewModel(LiveData, Factory) (0) | 2023.10.27 |
---|---|
[Android Java] CalendarPicker, TimePicker (Viewmodel, LiveData, Factory, EventBus) (2) | 2023.10.27 |
[Android Java] System.arraycopy (0) | 2023.10.27 |
[Android Java] URL 유무 및 추출 (0) | 2023.10.27 |
[Android Java] 문자열 공백 제거 trim(), replace() (0) | 2023.10.26 |