1

我是 android 新手,我无法理解 sqlite 数据库中的删除功能。我使用字符串创建了我的数据库

private static final String TABLE_NAME="NameInfo";
    private static final String POSITION_DB ="_id";
    private static final String Name_DB ="name";
    private static final String JSONOBJECT_DB="json";
    private static final String TIMESTAMP_DB="time";
    private static final String USERRATING_DB="userrating";
    private static final String DATABASE_NAME ="database.db";    
private final String createDb ="create table if not exists "+TABLE_NAME +" ("
                + POSITION_DB + " integer primary key, "
                + NAME_DB + " text not null, "
                + JSONOBJECT_DB + " text not null, "
                + USERRATING_DB + " text not null, "
                +TIMESTAMP_DB + " text not null); ";

现在,当我启动我的应用程序时,我希望删除超过 2 天前添加的所有行

所以我打算做这样的事情

long currentdate =new date().getTime();

然后检查表中每一行的 currenttime-Long.Valueof(TIMESTAMP_DB) 字段之间的差异,如果大于 2*24*60*60,则删除该行

有人可以告诉我如何使用以下功能来实现上述功能

public int delete (String table, String whereClause, String[] whereArgs)

我不确定我应该在 whereClause 和 whereArgs 中写什么。

如果有人能告诉我比这更好、更简单的方法,我将不胜感激。

PS我也试过用execSQL语句做,但不能写完整的查询database.execSQL("Delete * from "+TABLE_NAME+" WHERE = "+currentdate - Long.ValueOf(?) >2*24*60*60 ;")

提前致谢。

4

4 回答 4

2

您可以使用这样的查询:

db.execSQL("DELETE FROM " + TABLE_NAME +
           " WHERE " + currentdate + " - " + TIMESTAMP_DB + " > 2*24*60*60");

对于方法,在参数中的子句中update写SQL表达式;仅对字符串值需要:WHEREwhereClausewhereArgs

db.update(TABLE_NAME, currentdate + " - " + TIMESTAMP_DB + " > 2*24*60*60", null);
于 2013-07-29T06:38:52.103 回答
1

这是一个非常“巨大”的问题,并且可以有更多的工作方法取决于您的个人要求,例如安全性、性能等。

首先,我建议您使用参数化语句而不是您的“硬编码”,这些语句不安全且不易于人类阅读,因此请使用占位符 eq

select * from test where id = ?

其次,要从表中删除行,请使用 API 内置函数delete()。正如@Chintan Rathod 指出的那样,您的删除语句不是有效的语句。

现在回答你的问题。由于您想删除由于指定时间戳而导致的行,我建议您以特定日期格式插入具有指定日期的每一行(您可以简单地使用SimpleDateFormat。然后有更多方法可以做到这一点。我更喜欢首先查询一个表和放置简单的 if 条件,如果实际日期和存储在行中的日期相差 2 天,则保存他的 rowId。最后,您有要删除的行 ID。

但是,现在您并不确切知道要删除多少行,因此您需要:

  1. 循环删除记录(API小于11)
  2. IN使用子句创建动态查询
  3. 从 Android API 11 开始,有一种方法可以删除多条记录。由于 id 的大小(存储在某个动态数组中),您可以在此处创建“动态”语句。

例子:

第一种方法:

List<Integer> ids = new ArrayList<Integer>();
...
for (int id: ids) {
   db.delete("table", "_id = ?", new String[] {String.valueOf(id)});
}

第二种方法:

List<Integer> ids = new ArrayList<Integer>();
...
StringBuilder b = new StringBuilder("delete from table where _id IN(");
String[] whereArgs = new String[ids.size()];
int index = 0;
for (int id: ids) {
   whereArgs[index] = String.valueOf(id);
   b.append("?");
   if (index < ids.size() - 1) {
      b.append(",");
   }
   index++;
}
b.append(")");

db.execSQL(b.toString(), whereArgs);

第三种方法:

与上述相同,但您可以使用SQLiteStatement

SQLiteDatabase db; // instantiated SQLiteDatabase
SQLiteStatement stm = db.compileStatement(b.toString());
stm.executeUpdateDelete();


注意:由于您要删除多条记录(有时您需要删除 10 000 或更多行),请尝试考虑TRANSACTIONS哪些可以快速提高性能和安全性。

于 2013-07-29T06:54:29.010 回答
1

你也可以这样做::

db.execSQL(String.format("DELETE FROM %s WHERE %s = %d",
                Table_NAME,Table_ColumnName,Integer.parseInt(Value)));

它将直接执行该语句。

正如您所问的,WhereClause 表示您要使用的 ColumnNames 和表达式。其中 args 是字符串数组,因此您需要为您编写的 Expression 传递参数值。

db.delete(Table_NAME, ColumnName+" = ", String[]{Value});
于 2013-07-29T06:36:04.757 回答
0
public void deleteContact(Integer id) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete("AddressList",
                "id = ? ",
                new String[]{Integer.toString(id)});
}

或者

您可以使用原始 SQL 查询来删除行

public void deleteContact(Integer id) {
  SQLiteDatabase database = this.getWritableDatabase();
 String s = "DELETE FROM AddressList WHERE item_id=" + id;
database.execSQL(s);
}

这里AddressList是您的TABLE名称

于 2016-05-04T10:44:16.960 回答