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 days')
作为绑定参数传递,但这不起作用,因为 Android 文档 说:
这些值将被绑定为字符串。
参考文献:
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);
}
I have also tried passing in datetime('now', '+7 days')
as a bound parameter, that will not work, as the Android documentation says:
The values will be bound as Strings.
References:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
光标未关闭。
虽然这是有道理的,但真正让我困惑的是调用
moveToFirst()
(或其他能够以某种方式“处理”光标的函数)的要求。如果没有调用
moveToFirst()
和close()
,该行就永远不会更新。在rawQuery()
之后,close()
本身没有执行任何操作。The cursor was not closed.
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()
andclose()
, the row was never updated.close()
by itself, after therawQuery()
, did nothing.由于它是一个
UPDATE
语句,因此您可以使用execSQL()
而不是rawQuery()
。您不必担心游标(这对于UPDATE
语句来说有点愚蠢)。但是,您必须将值放入
WHERE
语句中,而不是传递参数,因为execSQL()
只接受 SQL 语句的单个字符串参数。另外,execSQL()
的类型为 void。我对除
SELECT
之外的几乎所有 SQL 语句都使用execSQL()
...Since it's an
UPDATE
statement you can useexecSQL()
rather thanrawQuery()
. You wouldn't have to bother with cursors (which is kinda silly for anUPDATE
statement).However, you will have to place values in your
WHERE
statement instead of passing args, asexecSQL()
only accepts a single String argument for your SQL statement. Also,execSQL()
is of type void.I use
execSQL()
for just about all SQL statements exceptSELECT
...