我试图查询我的SQLite数据库,但我收到错误"光标索引超出界限异常:请求索引0,大小为0"。当我在SQLite Man中运行相同的查询时,我得到了我正在寻找的结果。
public int getHighScore () {
String query = "SELECT score FROM " + SCORE_TABLE + ";";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
cursor.moveToFirst();
int score = cursor.getInt(0);
cursor.close();
return score;
}
答案 0 :(得分:0)
请检查游标是否为空,然后您要从游标中获取记录。使用此行获取记录: 的 cursor.getInt(cursor.getColumnIndex("得分")))强>
使用此代码:
public int getHighScore () {
int score=0;
String query = "SELECT score FROM " + SCORE_TABLE + ";";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor != null && cursor.moveToFirst()) {
score = cursor.getInt(cursor.getColumnIndex("score")));
}
cursor.close();
return score;
}