(隨意提高問題的標題,如果你能想到的更好的東西。)奇怪的行爲
問題:考慮以下SQLite的查詢:
SELECT COUNT(*)
FROM (SELECT 1 AS value UNION SELECT 2 AS value)
WHERE value <= ?
當我使用1
作爲參數,I 預計查詢產生1
,但它產生2
。爲什麼會發生?
信息:
這是一個最小的工作示例重現該問題(安卓):
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的工作,轉換值ap propriately。我還觀察到使用查詢的非參數化的版本(這可能是也可能不是相關的問題)時,以下幾點:
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
是有意義的。 char'1'= 49 – njzk2
也許你可以使用SELECT'1'和SELECT'2' – njzk2