Cursor index out of bounds

十年热恋 提交于 2020-01-25 07:20:05

问题


I am encountering cursor index out of bounds exception.

My code is as follows.

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();

I expect that the return value of the sql query is a number that's why I code the index to 0 as follows: cursor.getString(0)

But why am I encountering an cursor index out of bounds?


回答1:


Doc says rawQuery returns

A Cursor object, which is positioned before the first entry

So you seem to need call moveToFirst before using cursor.getString



来源:https://stackoverflow.com/questions/14058332/cursor-index-out-of-bounds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!