0

我遇到游标索引越界异常。

我的代码如下。

SQLiteDatabase db = this.getWritableDatabase();
String completedInList = "SELECT COUNT(*) FROM " + TABLE_BUCKET_ITEMS + " WHERE "
                + KEY_BUCKET + " = " + bucketNo + " AND " + KEY_FLAG + " = 1" ;

Cursor cursor = db.rawQuery(completedInList, null);
int completed = Integer.parseInt(cursor.getString(0));

String totalNoList = "SELECT COUNT(*) FROM " + TABLE_BUCKET_ITEMS + " WHERE "
                + KEY_BUCKET + " = " + bucketNo;
cursor.close();

Cursor cursor2 = db.rawQuery(totalNoList, null);
int total = Integer.parseInt(cursor2.getString(0));

float percentage = ((float)completed) / total;

cursor2.close();
db.close();

我希望 sql 查询的返回值是一个数字,这就是我将索引编码为 0 的原因,如下所示:cursor.getString(0)

但是为什么我遇到游标索引超出范围?

4

1 回答 1

3

医生rawQuery退货

一个 Cursor 对象,位于第一个条目之前

所以你似乎需要 moveToFirst在使用前打电话cursor.getString

于 2012-12-27T17:12:02.277 回答