如何将多个参数传递给Android中的查询?

时间:2012-04-11 08:00:08

标签: android sql sqlite

我想将多个参数传递给Android中的查询。 我做了以下事情:

String final QUERY = "SELECT TA.ID FROM TAB TA LEFT JOIN PROP P ON P.ID = TA.ID LEFT JOIN LOC L ON L.ID = TA.ID LEFT JOIN TIME T ON T.ID = TA.ID  WHERE (COALESCE(LONG,?) - ?) * (COALESCE(LONG,?) - ?) + (COALESCE(LAT,?) - ?)  * (COALESCE(LAT,?) - ?) <=COALESCE(VALUE,0)*COALESCE(VALUE,0)  AND ? BETWEEN  COALESCE(BEGIN,0) AND COALESCE(END, 123456)";
    String[] args= {"" + val1, "" + val2, "" + val3, "" + val4, "" + val5, "" val6, "" + val7, "" + val8, "" + val9};
    cursor = db.rawQuery(QUERY,args);

但它重新打造了一个空光标。我在shell中尝试了查询,它可以工作。如何在代码中实现它?

修改: 我找到了另一种方法:

QUERY = "SELECT TA.ID FROM TAB TA LEFT JOIN PROP P ON P.ID = TA.ID LEFT JOIN LOC L ON L.ID = TA.ID LEFT JOIN TIME T ON T.ID = TA.ID  WHERE (COALESCE(LONG," + val1+ " ) - " + val2 + " ) * (COALESCE(LONG," + val3 + " ) - " + val4 + " ) + (COALESCE(LAT," + val5 + " ) - " +  val6 + ")  * (COALESCE(LAT," +  val7 + " ) - " + val8 + ") <=COALESCE(VALUE,0)*COALESCE(VALUE,0)  AND" +  val9 + " BETWEEN  COALESCE(BEGIN,0) AND COALESCE(END, 123456)";
  cursor = db.rawQuery(QUERY,null);

1 个答案:

答案 0 :(得分:0)

你犯了一些错误和不精确。 为了提高性能,最好使用:

String.valueOf(valX);

然后,如果你看看android SQLiteDatabase文档,你会看到方法rawQuery(String sql,String [] selectionArgs)接受sql加上selectionArgs那些不是你的whereArgs。