我有一个从数据库读取数据的函数。但这总是返回错误..我对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);
}
有帮助吗?

解决方案

用这个:

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
}

其他提示

这是正确的代码

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
.

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 "";
.

声音,如您正在尝试从空结果集读取。

检查数据库,查询返回大小0的光标,因此您正在尝试在索引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 "";
.

您正在将界限偏出界限异常,因为您通过的记录数代替列号。 作为最佳编码实践,检查游标进行记录,即它为空或包含记录。 使用此

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
}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top