我正在尝试从我的sqlite数据库中获取一行。当我运行此行代码时:-
RReckoner_info rReckoner_info = dbHandler.get_subcategory(1);
Log.d("INFO : ", rReckoner_info.getCategory_name());
我收到以下错误:
android.database.CursorIndexOutOfBoundsException:已请求索引0,
大小为0
public RReckoner_info get_subcategory(int id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_SUBCATEGORY_LIST, new String[]{KEY_NAME,KEY_DESC, KEY_CATEGORY_ID, KEY_CONTENT1,KEY_CONTENTTYPE1,KEY_CONTENT2,KEY_CONTENTTYPE2,KEY_CONTENT3,KEY_CONTENTTYPE3,
KEY_CONTENT4,KEY_CONTENTTYPE4,KEY_CONTENT5,KEY_CONTENTTYPE5,KEY_CONTENT6,KEY_CONTENTTYPE6,KEY_ORDERID,KEY_STATUS ,KEY_UPDATED},KEY_NAME +"=?",
new String[]{String.valueOf(id)},null,null, null,null);
if (cursor !=null)
{
cursor.moveToFirst();
}
RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));
return rReckoner_info;
}
谁能帮助我解决这个问题。感谢您的帮助或建议。谢谢。
最佳答案
更改以下代码
来自:
if (cursor !=null)
{
cursor.moveToFirst();
}
RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));
return rReckoner_info;
至 :
if (cursor !=null && cursor.getCount() > 0)
{
cursor.moveToFirst();
RReckoner_info rReckoner_info = new RReckoner_info(cursor.getString(0),cursor.getString(1),Integer.parseInt(cursor.getString(2)),
cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)
,cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),
cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),
Integer.parseInt(cursor.getString(15)),cursor.getString(16),cursor.getString(17));
return rReckoner_info;
}
return null;
如果找不到行,我将返回
null
,尽管您可以根据需要更改最后一部分,但是必须检查光标的count
以避免CursorIndexOutOfBoundsException
。