开发者

java.lang.IllegalStateException error in Android Sqlite3 "group by" clause

开发者 https://www.devze.com 2023-04-11 14:55 出处:网络
This is my query in sqlite3 by adb shell: sqlite> SELECT round FROM prizes GROUP BY round; 100-7 However, I got a problem when translating it into Java code:

This is my query in sqlite3 by adb shell:

sqlite> SELECT round FROM prizes GROUP BY round;
100-7

However, I got a problem when translating it into Java code:

LinkedList<String> result = new LinkedList<String&开发者_开发知识库gt;();
Cursor cursor = db.rawQuery("SELECT round FROM prizes GROUP BY round", null);
if(cursor.moveToFirst()) {
    do {
        result.add(cursor.getString(1));
    } while (cursor.moveToLast());
}
if (cursor != null && !cursor.isClosed())
    cursor.close();

return (String[]) result.toArray();

When I run this code, I got an error:

ERROR/AndroidRuntime(10540): java.lang.IllegalStateException: get field slot from row 0 col 1 failed


This is because you have only 1 column returned form your query (round) and the index is zero-based

See the doc about getString

Returns the value of the requested column as a String. The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined. Parameters

columnIndex the zero-based index of the target column.

The below should work:

  result.add(cursor.getString(0));


I believe it should be cursor.getString(0) since its a 0-based index.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号