Android 从SQLite数据库中删除数据

Android 从SQLite数据库中删除数据,android,sqlite,Android,Sqlite,嗨,我正在安卓系统中使用SQLite 我试图通过这样做从数据库中删除旧帖子 this.db.delete( EVENT_TABLE_NAME, "date < ?", new String[] {String.valueOf(limit.getTime())} ); this.db.delete( 事件\u表\u名称, “日期

嗨,我正在安卓系统中使用SQLite

我试图通过这样做从数据库中删除旧帖子

this.db.delete(
  EVENT_TABLE_NAME,
  "date < ?", 
  new String[] {String.valueOf(limit.getTime())}
);
this.db.delete(
事件\u表\u名称,
“日期<?”,
新字符串[]{String.valueOf(limit.getTime())}
);
其中limit是从日历实例中获取的当前日期,减去21600000毫秒

但这似乎删除了所有帖子。

我的猜测是:

context.deleteDatabase(数据库名称)

试试这个

this.db.delete(
  EVENT_TABLE_NAME,
  "date < "+limit.getTime(),null);
this.db.delete(
事件\u表\u名称,
“日期<”+limit.getTime(),null);

您可以尝试以下方法:

public static String getDateStringFromDate(Date date, String pattern,
                                           String timezone) {
    // Set locale to US to prevent date issues
    if (!Locale.getDefault().equals(Locale.US)) {
        Locale.setDefault(Locale.US);
    }
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    if (timezone != null) {
        df.setTimeZone(TimeZone.getTimeZone(timezone));
    }
    if (date == null) {
        return null;
    }
    try {
        return df.format(date);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
this.db.delete(
    EVENT_TABLE_NAME,
    "date < ?", 
    new String[] {getDateStringFromDate(date,pattern,timezone)}
此函数将使用自定义日期格式创建字符串,您可以创建函数查询:) 之后,您可以像这样使用删除功能:

public static String getDateStringFromDate(Date date, String pattern,
                                           String timezone) {
    // Set locale to US to prevent date issues
    if (!Locale.getDefault().equals(Locale.US)) {
        Locale.setDefault(Locale.US);
    }
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    if (timezone != null) {
        df.setTimeZone(TimeZone.getTimeZone(timezone));
    }
    if (date == null) {
        return null;
    }
    try {
        return df.format(date);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
this.db.delete(
    EVENT_TABLE_NAME,
    "date < ?", 
    new String[] {getDateStringFromDate(date,pattern,timezone)}
this.db.delete(
事件\u表\u名称,
“日期<?”,
新字符串[]{getDateStringFromDate(日期、模式、时区)}

))

这对我有用:

    public static boolean deleteData(String tableName,String where, String[] value){
    sqliteDb = instance.getWritableDatabase();
    sqliteDb.delete(tableName, where, value);
    return true;
    }

不,那绝对不是我想要的。我想删除一篇帖子。不是整个数据库