android.database.sqlite.SQLiteDatabase.rawQuery()不使用SQLite datetime()函数更新DATETIME列

问题描述:

public Cursor set_datetime_next(Reminder r) {       
    String _newVal = "datetime('now', '+7 days')";
    String[] args = { new Integer(r.getID()).toString() };
    String query =
        "UPDATE " + DBConst.TABLE
      + " SET "   + DBConst.f_DATETIME_NEXT + "=" + _newVal
      + " WHERE " + DBConst.f_ID +"=?";
    Log.i(TAG, query);
    return db.rawQuery(query, args);
}

我还尝试传递 datetime('now ','+7天')作为绑定参数,这将不起作用,因为 Android文档说:

I have also tried passing in datetime('now', '+7 days') as a bound parameter, that will not work, as the Android documentation says:

值将绑定为字符串。

参考文献:

  • http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
  • http://www.sqlite.org/lang_datefunc.html

游标未关闭。

public void set_datetime_next(Reminder r, String _newVal) {     
    String[] args = { new Integer(r.getID()).toString() };
    String query =
        "UPDATE " + DBConst.TABLE
      + " SET "   + DBConst.f_DATETIME_NEXT + "=" + _newVal
      + " WHERE " + DBConst.f_ID +"=?";
    Log.i(TAG, query);
    Cursor cu = db.rawQuery(query, args);
    cu.moveToFirst();
    cu.close();     
}

虽然这是有道理的,但真正令我困惑的是需要调用 moveToFirst()(或某些其他功能将以某种方式使用光标)。

无需调用 moveToFirst() close(),该行从未更新。 close() rawQuery()之后,没有任何操作。

While that makes sense, what really puzzles me is the requirement of calling moveToFirst() (or some other function which would "work with" the cursor in some way).
Without the call to both moveToFirst() and close(), the row was never updated. close() by itself, after the rawQuery(), did nothing.