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)
但爲什麼我遇到一個遊標索引越界?
之前調用
moveToFirst
你應該注意的是,從查詢返回的遊標在其結果索引之前初始化*。您至少必須調用cursor.moveToFirst();或其他移動方法來移動到查詢行中。但是,這也可能是由於在查詢中沒有返回任何結果,並且無論如何嘗試訪問列。這是別人需要注意的。 – mango