我在我的應用程序之一中使用Ormlite數據庫。現在我想從數據庫中刪除特定日期之前存儲的所有記錄。我們舉一個例子: -在特定日期之前刪除使用ORMlite的所有記錄
今天是4月17日。當用戶在通過應用程序的選項之一中選擇了一週時,則應該從數據庫中刪除在一週之前(即4月10日之前和之前)的所有記錄。
以下是我的目標bean類: -
public class AlertsDataClass extends BaseDataClass implements Parcelable {
public static final Creator<AlertsDataClass> CREATOR = new Creator<AlertsDataClass>() {
@Override
public AlertsDataClass createFromParcel(Parcel in) {
return new AlertsDataClass(in);
}
@Override
public AlertsDataClass[] newArray(int size) {
return new AlertsDataClass[size];
}
};
@DatabaseField
private int alertId;
@DatabaseField
private String alertTitle;
@DatabaseField
private int alertLevel;
@DatabaseField
private String alertLog;
@DatabaseField
private int alertAssignee;
@DatabaseField
private String alertLocationId;
@DatabaseField
private int alertStatus;
@DatabaseField
private String alertStartTime;
@DatabaseField
private String alertUpdatedTime;
@DatabaseField
private String alertDeviceId;
@DatabaseField
private int alertActionNeeded;
@DatabaseField
private String alertAssigneeName;
public AlertsDataClass() {
}
protected AlertsDataClass(Parcel in) {
alertId = in.readInt();
alertTitle = in.readString();
alertLevel = in.readInt();
alertLog = in.readString();
alertAssignee = in.readInt();
alertAssigneeName = in.readString();
alertLocationId = in.readString();
alertStatus = in.readInt();
alertStartTime = in.readString();
alertUpdatedTime = in.readString();
alertDeviceId = in.readString();
alertActionNeeded = in.readInt();
}
public int getAlertId() {
return alertId;
}
public void setAlertId(int alertId) {
this.alertId = alertId;
}
public String getAlertTitle() {
return alertTitle;
}
public void setAlertTitle(String alertTitle) {
this.alertTitle = alertTitle;
}
public int getAlertLevel() {
return alertLevel;
}
public void setAlertLevel(int alertLevel) {
this.alertLevel = alertLevel;
}
public String getAlertLog() {
return alertLog;
}
public void setAlertLog(String alertLog) {
this.alertLog = alertLog;
}
public int getAlertAssignee() {
return alertAssignee;
}
public void setAlertAssignee(int alertAssignee) {
this.alertAssignee = alertAssignee;
}
public String getAlertLocationId() {
return alertLocationId;
}
public void setAlertLocationId(String alertLocationId) {
this.alertLocationId = alertLocationId;
}
public int getAlertStatus() {
return alertStatus;
}
public void setAlertStatus(int alertStatus) {
this.alertStatus = alertStatus;
}
public String getAlertStartTime() {
return alertStartTime;
}
public void setAlertStartTime(String alertStartTime) {
this.alertStartTime = alertStartTime;
}
public String getAlertUpdatedTime() {
return alertUpdatedTime;
}
public void setAlertUpdatedTime(String alertUpdatedTime) {
this.alertUpdatedTime = alertUpdatedTime;
}
public String getAlertDeviceId() {
return alertDeviceId;
}
public void setAlertDeviceId(String alertDeviceId) {
this.alertDeviceId = alertDeviceId;
}
public int getAlertActionNeeded() {
return alertActionNeeded;
}
public void setAlertActionNeeded(int alertActionNeeded) {
this.alertActionNeeded = alertActionNeeded;
}
public String getAlertAssigneeName() {
return alertAssigneeName;
}
public void setAlertAssigneeName(String alertAssigneeName) {
this.alertAssigneeName = alertAssigneeName;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(alertId);
dest.writeString(alertTitle);
dest.writeInt(alertLevel);
dest.writeString(alertLog);
dest.writeInt(alertAssignee);
dest.writeString(alertAssigneeName);
dest.writeString(alertLocationId);
dest.writeInt(alertStatus);
dest.writeString(alertStartTime);
dest.writeString(alertUpdatedTime);
dest.writeString(alertDeviceId);
dest.writeInt(alertActionNeeded);
}
}
在上面的數據類,「alertStartTime」是包含在「17年4月17日的格式的日期字段16:29" 。現在,當用戶選擇一週時,應該從數據庫中刪除「04/10/17 16:29」之前和之前的所有記錄。
我正在刪除基於兩個字段的記錄。一個是「alertStatus」,其值應該是5,另一個是「alertStartTime」,在該日期之前一週將刪除所有在「04/10/17 16:29」之前的記錄。我希望你們能清楚地理解我的問題。
我的數據庫中刪除記錄的方法如下: -
public void deleteCompleteAlerts(int alertStatus, String alertStartTime) {
DeleteBuilder<AlertsDataClass, Integer> deleteBuilder = getAlertsDetailDao().deleteBuilder();
try {
Where<AlertsDataClass, Integer> where = deleteBuilder.where();
int condCount = 0;
if(alertStatus == 5){
where.eq("alertStatus", alertStatus);
condCount++;
}
if(alertStartTime.equalsIgnoreCase("04/14/17")){
where.like("alertStartTime", "%" + alertStartTime + "%");
condCount++;
}
if (condCount > 0) {
where.and(condCount);
}
deleteBuilder.delete();
} catch (SQLException e) {
e.printStackTrace();
}
}
在上述方法中,我們要修改這個if語句如下: -
if(alertStartTime.equalsIgnoreCase("04/14/17")){
where.like("alertStartTime", "%" + alertStartTime + "%");
condCount++;
}
請幫我弄清楚這個問題。謝謝先進的傢伙!