0

我有一个从db读取数据的功能。但这总是返回错误..我对android了解不多..寻求帮助

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);
}
4

7 回答 7

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
}
于 2012-10-03T06:17:55.913 回答
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
于 2012-10-03T06:18:24.930 回答
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 "";
于 2012-10-03T06:18:44.780 回答
0

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

于 2012-10-03T06:19:18.750 回答
0

检查您的数据库,查询返回大小为 0 的游标,因此您试图访问索引 0 处的游标,因此出现错误。重新考虑查询并检查数据库中的值。

于 2012-10-03T06:20:42.950 回答
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 "";
于 2012-10-03T06:23:34.397 回答
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
}
于 2012-10-03T06:25:04.020 回答