(如果您能想到更好的方法,请随时改进问题标题。)
问题:考虑以下 SQLite 查询:
SELECT COUNT(*)
FROM (SELECT 1 AS value UNION SELECT 2 AS value)
WHERE value <= ?
当我1
用作参数时,我希望查询 yield 1
,但它 yield 2
。为什么会这样?
附加信息:
这是重现问题的最小工作示例(Android):
Cursor c = db.rawQuery(
"SELECT COUNT(*) " +
" FROM (SELECT 1 AS value UNION SELECT 2 AS value) " +
" WHERE value <= ? ",
new String[] {String.valueOf(1)});
c.moveToFirst();
Log.i("", "Result: " + String.valueOf(c.getInt(0)));
这可能与参数作为字符串传递的事实有关,但是,唉,没有其他方法可以将参数传递给 SQLite API,所以我想我在这里没有做任何“错误”,它是 SQLite 的工作以适当地转换值。在使用查询的非参数化版本时,我还观察到以下情况(这可能与问题相关,也可能不相关):
SELECT COUNT(*)
FROM (SELECT 1 AS value UNION SELECT 2 AS value)
WHERE value <= 1 -- yields 1
SELECT COUNT(*)
FROM (SELECT 1 AS value UNION SELECT 2 AS value)
WHERE value <= '1' -- yields 2