0

我正在使用以下内容,AuthorName我想在其中找到列值AuthorIDcatid是一个数组,包含AuthorID

public Cursor Authorname(String[] catid)   {
   myDataBase.rawQuery("SELECT AuthorName FROM AUTHOR_NAME WHERE AuthorID = ?",catid);
}

但它回来了IllegalArgumentException。任何人都可以帮我缩短这个。提前致谢。

4

2 回答 2

3

您只能使用一个查询来获取所有用户。这是一个例子。

private static final String QUERY = "SELECT AuthorName FROM AUTHOR_NAME WHERE AuthorID IN %ids";

public Cursor Authorname(String[] catid) {

    // Build the string with all the IDs, e.g. "(1,2,3,4)"
    StringBuilder ids = new StringBuilder();
    ids.append("(");
    for(int i = 0; i < catid.length; i++) {
        ids.append(String.valueOf(catid[i]);
        if (i < catid.length - 1) {
            ids.append(",");
        }
    }
    ids.append(")");

    // Use the string with the ids in the query
    String finalQuery = QUERY.replaceAll("%ids", ids.toString());

    // Execute the query
    return myDataBase.rawQuery(finalQuery);
}
于 2013-01-11T10:12:58.310 回答
-3

更改您的查询

myDataBase.rawQuery("SELECT AuthorName FROM AUTHOR_NAME WHERE AuthorID = ?",catid);

如下

cursor = myDataBase.query("AUTHOR_NAME",
                    new String[] {"AuthorName"}, "AuthorID IN(?)", catid, null, null, null);

试试上面的解决方案希望它对你有用。

于 2013-01-11T10:04:58.157 回答