如何在Android中编写Sql RawQuery

时间:2014-08-04 10:07:13

标签: android sql sqlite

我有一个在Sqlite浏览器中工作的查询,但是当我在Android中编写它时,它无法正常工作。下面是我写的SQL查询

SELECT value 
FROM current_tab 
WHERE variable = 'currentVehicleSpeed' 
  AND ABS(1 - value) = (SELECT MIN(ABS(1 - value)) 
                        FROM current_tab 
                        WHERE variable = 'currentVehicleSpeed' 
                          AND ABS(1 - value) <= 5)

我想要db.rawQuery(...)内的上述查询。

我就是这样做的:

db.rawQuery("SELECT value FROM current_tab WHERE variable = '" + var + "' AND ABS( '"
                        + valueDouble + "' - value )=(SELECT MIN(ABS( '"
                        + valueDouble + "' - value )) FROM current_tab WHERE variable = '" + var + "') AND ABS( '" + valueDouble + "' - value <=5) ", null);

其中&#39;值&#39;和&#39;变量&#39;是列名

current_tab是表名

var = "currentVehicleSpeed" and valueDouble = 1

感谢您的帮助

1 个答案:

答案 0 :(得分:-1)

您不必在数字上使用文字分隔符('):

您的正确查询:

db.rawQuery("SELECT value FROM current_tab WHERE variable = " + var + " AND ABS("
    + valueDouble + " - value) = (SELECT MIN(ABS("
    + valueDouble + " - value)) FROM current_tab WHERE variable = " + var + ") AND ABS(" + valueDouble + " - value <= 5)", null);