游标索引超出范围 [英] Cursor index out of bounds

查看:88
本文介绍了游标索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到游标索引超出范围例外。

I am encountering cursor index out of bounds exception.

我的代码如下。

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)

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)

但是为什么我遇到一个超出范围的游标索引? / p>

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

推荐答案

文档 rawQuery 返回


光标对象,它位于第一个条目之前

A Cursor object, which is positioned before the first entry

因此,在使用 cursor.getString

这篇关于游标索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆