0

我正在尝试使用 bindargs 运行以下查询,但似乎它们没有被放入查询中。

String questionWhereClause = Article.COL_CATEGORY_ID + " = ? AND " + Article.COL_TOPIC_ID + " = ? AND " + Article.COL_QUESTION_ID + " = ?";
String[] questionSelectionArgs = new String[] { Integer.toString(category.getId()), Integer.toString(topic.getId()), Integer.toString(question.getId()) };

Cursor cursor = mDBConnection.query(Article.TABLE_NAME, null, questionWhereClause , questionSelectionArgs , null, null, null);

// Use cursor here

如果我?用适当的值替换光标返回正常。当我去使用cursor.moveToNext()bindargs,光标中没有项目。当我查看光标对象时,下面的值mCompiledSqlStatement也仍然有?标记。对此的任何帮助将不胜感激。

4

1 回答 1

1

The reference of SQLiteDatabase says:

You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.

I'm guessing that this last bit about the values bound as Strings could be the cause of the issue somehow.

In any ase I have a workaround for you: use the rawQuery method instead, crafting your SQL string like this:

String sql = String.format("SELECT * FROM %s WHERE %s = %d AND %s = %d AND %s = %d",
     Article.TABLE_NAME, 
     Article.COL_CATEGORY_ID, category.getId(),
     Article.COL_TOPIC_ID, topic.getId(),
     Article.COL_QUESTION_ID, question.getId());

This is not as efficient as binding the parameters (since here we cannot benefit from prepared statements), but for all practical purposes, it's probably good enough. I use this style quite a lot, as I find it easier to read.

Update

The equivalent of the following also works for me in one of my apps:

String sql = String.format("SELECT * FROM %s WHERE %s = ? AND %s = ? AND %s = ?",
     Article.TABLE_NAME, 
     Article.COL_CATEGORY_ID, 
     Article.COL_TOPIC_ID, 
     Article.COL_QUESTION_ID);
String args = new String[] { category.getId(), topic.getId(), question.getId() };
Cursor cursor = getReadableDatabase().rawQuery(sql, args);

That is, binding arguments works for me, whether they are text columns or integer columns. I hope you can get it to work too.

于 2013-07-15T20:50:53.203 回答