游标索引超出范围

时间:2012-12-27 17:02:54

标签: android cursor

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

我的代码如下。

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)

但为什么我遇到一个超出界限的游标索引?

1 个答案:

答案 0 :(得分:3)

DocrawQuery返回

  

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

之前

因此,在使用moveToFirst

之前,您似乎需要致电cursor.getString