我在我的一个应用程序中使用Ormlite数据库。现在我想删除数据库中存储在特定日期之前的所有记录。让我们举一个例子: -
今天是4月17日。当用户在其中一个选项中选择一周时,应该从数据库中删除一周之前,即4月10日之前和之前的所有记录。
以下是我的Object 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);
}
}
在上面的数据类中,&#34; alertStartTime&#34;是包含&#34; 04/17/17 16:29&#34;格式的日期的字段。现在,当用户选择一周时,所有记录在&#34; 04/10/17 16:29&#34;之前和之后应该从数据库中删除。
我在两个字段的基础上删除记录。一个是&#34; alertStatus&#34;其值应为5,另一个为&#34; alertStartTime&#34;在提前一周,shuold被删除之前和之后的所有记录&#34; 04/10/17 16:29&#34;。我希望你们能清楚地理解我的问题。
我的数据库删除记录方法如下: -
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++;
}
请帮我解决这个问题。先谢谢你们!!
答案 0 :(得分:1)
在上面的数据类中,&#34; alertStartTime&#34;是包含&#34; 04/17/17 16:29&#34;格式的日期的字段。现在,当用户选择一周时,所有记录在&#34; 04/10/17 16:29&#34;之前和之后应该从数据库中删除。
您应该可以执行以下操作:
DeleteBuilder<AlertsDataClass, Integer> db = dao.deleteBuilder();
db.where().le("alertStartTime", "2017-04-10 16:29");
db.delete();
Sqlite处理非常specific timestamp formats,在您的情况下,它需要采用"YYYY-MM-DD HH:MM"
格式。来自Sqlite's docs about time formats的更多内容。