1

我想将多个参数传递给 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);
4

1 回答 1

0

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

String.valueOf(valX);

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

于 2012-04-11T09:30:03.737 回答