使用selectionArgs没有给出预期的结果

时间:2013-11-19 17:03:50

标签: android sql

在过去的几年里,我对SQL非常熟悉......但是这个特殊情况让我非常难过。

简而言之,这怎么可能:

int totalMinutes = (cal.get(Calendar.HOUR_OF_DAY) * 60) + cal.get(Calendar.MINUTE);

Cursor timeCheck = mDatabase.query("dailys", null, "dueToday = 1 and occursAtTime = 1 and (minutesOfDay + snoozeDelay) < " + totalMinutes + " and completed = 0 and stopRemindingToday = 0", null, null, null, null);

和此:

int totalMinutes = (cal.get(Calendar.HOUR_OF_DAY) * 60) + cal.get(Calendar.MINUTE);

String [] totalMinutesToday = {Integer.toString(totalMinutes)};

Cursor timeCheck = mDatabase.query("dailys", null, "dueToday = 1 and occursAtTime = 1 and (minutesOfDay + snoozeDelay) < ? and completed = 0 and stopRemindingToday = 0", totalMinutesToday, null, null, null);

给出不同的结果? minutesOfDay和snoozeDelay都是整数类型,据我所知,我从来没有遇到过这样的情况。第二个示例返回一个结果,在仔细检查每列的数据之后,绝对不应该是有效的结果。 minutesOfDay + snoozeDelay是660,在测试时,变量totalMinutes(当使用简单的android记录时)介于490和500之间。

为了澄清,第一个示例没有返回任何结果,这是基于数据的预期结果。

修改

我对这里的帖子进行了一些研究,这显然也有效:

int totalMinutes = (cal.get(Calendar.HOUR_OF_DAY) * 60) + cal.get(Calendar.MINUTE);

String [] totalMinutesToday = {Integer.toString(totalMinutes)};

Cursor timeCheck = mDatabase.query("dailys", null, "dueToday = 1 and occursAtTime = 1 and (minutesOfDay + snoozeDelay) < CAST(? AS INTEGER) and completed = 0 and stopRemindingToday = 0", totalMinutesToday, null, null, null);

仍然想知道为什么这是必要的。我不记得以前必须通过选择参数传递的整数值。正在过去的值是否被转换为围绕它的单引号?

0 个答案:

没有答案