SQLite查询 - 日期保留字?

时间:2013-07-01 07:58:09

标签: android sqlite

我只是好奇,dateSQLite的保留字吗?我有一个名为date的列,并尝试运行此查询:

DELETE FROM cases WHERE date <= date('now','-1 day')

通过这样做:

String query = "DELETE FROM cases WHERE date <= date('now','-1 day')";

        SQLiteDatabase db = this.getWritableDatabase(); 

        Cursor cursor = db.rawQuery(query, null);
        boolean found = cursor.moveToFirst(); 
        if(found) {
            int result = cursor.getCount();
            Log.w("DeleteOldCases: ", "Result: " + Integer.toString(result));
            db.close();
        }

但是当调用moveToFirst时,光标只会给我一个错误的回复。但是在我的数据库中,实际上存在超过一天的行。任何人都可以解释这里有什么问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

是的,这似乎是一个保守的词。

看看:http://www.sqlite.org/lang_datefunc.html

 date(timestring, modifier, modifier, ...) 

有一个游标形成DELETE语句很奇怪。尝试在

中拆分操作
SELECT * FROM cases WHERE date <= date('now','-1 day')

然后执行检查

SQLiteDatabase db = this.getWritableDatabase(); 
Cursor cursor = db.rawQuery(query, null);
boolean found = cursor.moveToFirst(); 
if(found) {
    int result = cursor.getCount();
    Log.w("DeleteOldCases: ", "Result: " + Integer.toString(result));
    db.close();
}

然后执行DELETE部分

DELETE FROM cases WHERE date <= date('now','-1 day')