获取CursorIndexOutOfBoundsException

时间:2012-10-03 06:13:02

标签: android sqlite indexoutofboundsexception

我有一个从db读取数据的函数。但这总是返回错误..我没有太多的知识在android..looking寻求帮助

10-03 11:33:05.870: E/AndroidRuntime(1321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.projects.myworldsafe/com.projects.myworldsafe.DeailedView}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

String getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content  FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);   
 c.moveToFirst();
 int index= c.getCount();
 int indexd= c.getColumnIndex(ITEM_CONTENT); 
 System.out.print("Count query"+indexd);
 return c.getString(index);
}

7 个答案:

答案 0 :(得分:2)

使用它:

return c.getString(indexd);

这是你真正想要获得的价值。你在那里使用index,它返回光标中的记录数。

更好的做法是:

String getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content  FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);   
if(c.getCount()>0){
    c.moveToFirst();
    int index= c.getCount();
    int indexd= c.getColumnIndex(ITEM_CONTENT); 
    System.out.print("Count query"+index);// use index instead of indexd to print total records in query
    return c.getString(index);// return value of column number specified in indexd
}
else
    return "";// when no records found
}

答案 1 :(得分:0)

这是正确的代码

int index= c.getCount(); // this will return total rows found
int indexd= c.getColumnIndex(ITEM_CONTENT);  // this is the column index by giving the column name.
System.out.print("Count query"+indexd);
return c.getString(indexd); // here you need to pass the column index but you passing the index which was total number of rows

答案 2 :(得分:0)

Cursor c=db.rawQuery(selectQuery,params);   

返回大小为0的光标,更改代码以检查光标是否有项目,然后通过以下方式移动到第一个位置:

Cursor c=db.rawQuery(selectQuery,params);   
if(c.getCount()>0)
{
 c.moveToFirst();
int index= c.getCount();
 int indexd= c.getColumnIndex(ITEM_CONTENT); 
 System.out.print("Count query"+indexd);
 return c.getString(index);
}
return "";

答案 3 :(得分:0)

听起来你正试图从空的结果集中读取。

答案 4 :(得分:0)

检查你的数据库,查询返回一个大小为0的游标,所以你试图在索引0处访问游标,因此错误。 重新思考查询并检查数据库中的值。

答案 5 :(得分:0)

您可能正在访问0行光标。改为:

// moveToFirst itself will return false if there is no entry on Cursor
if(c.moveToFirst()){
    int cursorCount = c.getCount();
    int indexd = c.getColumnIndex(ITEM_CONTENT); 
    System.out.print("Count query"+index);
    // You should getString(ColumnIndex) not CursorCount
    return c.getString(indexd);
} else
    return "";

答案 6 :(得分:0)

当您传递记录数而不是列号时,您将获得索引超出范围的异常。 作为最佳编码实践,检查光标是否有记录,即它是空的还是包含记录。 使用此

String getItemDetailsWp(String id){
System.out.print(id);
SQLiteDatabase db = this.getReadableDatabase();
String[] params=new String[]{id};
String selectQuery = "SELECT Content  FROM " + TABLE_ITEMS +" WHERE "+ITEM_ID+"=?";
Cursor c=db.rawQuery(selectQuery,params);   
if(c.getCount()>0){
c.moveToFirst();
int index= c.getCount();
int indexd= c.getColumnIndex(ITEM_CONTENT); 
System.out.print("Count query"+index);
return c.getString(indexd);
}
else
return "";// when no records found
}