android.database.sqlite.SQLiteDatabase.rawQuery()不使用SQLite datetime()函数更新DATETIME列 [英] android.database.sqlite.SQLiteDatabase.rawQuery() is not updating a DATETIME column with a SQLite datetime() function

查看:138
本文介绍了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.

这篇关于android.database.sqlite.SQLiteDatabase.rawQuery()不使用SQLite datetime()函数更新DATETIME列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆