Android sqlite db查询无法获取正确的数据

时间:2015-03-12 10:37:22

标签: android sqlite

我有一个问题。

有这样一张桌子:

(table name: times)
+-------+-----------+---------+
| block | startTime | endTime |
+-------+-----------+---------+
|     1 | 08:00     | 10:00   |
+-------+-----------+---------+

当我像这样使用rawquery时:

String ctime = "'09:45'";
cursor = sdb.rawQuery(String.format("select block from times where startTime < time(%s) and endTime> time(%s)", ctime,ctime), null);

我可以获得正确的数据“1”。

但是当我使用这样的查询时:

cursor = sdb.query("times", new String[]{"block"}, "startTime <= time(?) and EndTime >= time(?)", new String[]{ctime,ctime}, null, null, null);

我无法获得正确的数据。 光标计数为0。

为什么?

1 个答案:

答案 0 :(得分:1)

有效的时间字符串不得包含引号。

当您手动构造SQL语句时,ctime变量中的引号是正确的,但它们用于分隔SQL字符串,而不是SQL字符串值的一部分。

当您使用参数时,您不需要在字符串周围加上引号(除非您确实希望它们成为字符串值的一部分)。