查询动态selectionArgs android SQLite

时间:2015-10-06 07:39:06

标签: java android sqlite cursor

我正在尝试返回一个游标,其中所有行都匹配String Array ID中的一个id - 这就是我到目前为止我得到的错误。我认为这与它只有1'的事实有关?但是我怎么能让这个动态呢?有什么建议吗?

public Cursor getSelectedEntries(String... ids)
{

    return movieDbInstance.query(
            DATABASE_TABLE,
            new String[]{_ID, MOVIE_ID, MOVIE_TITLE, MOVIE_YEAR, MOVIE_POSTER},
                MOVIE_ID + " = ?",
            ids,
            null,
            null,
            null);
}

例如,String数组的id数量正在变化 它可能是{“abc”,“def”}或它可能是{“abc”,“def”,“gef”,“gdf”}我需要返回MOVIE_ID =其中一个的所有行...

错误消息` java.lang.IllegalArgumentException:无法绑定索引10处的参数,因为索引超出范围。
该语句有1个参数。在android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)

1 个答案:

答案 0 :(得分:1)

You are passing an array of ids, but have only one placeholder in your WHERE-Statement. If you have several ids, you need to put several placeholders in the WHERE-clause like:

MOVIE_ID + " = ? OR " + MOVIE_ID + " = ? OR " + ...

The number of placeholders of course needs to match the number of ids in you array. So if the number of ids changes, you need to construct your WHERE clause dynamically like above.