由于CursorWindow,Database Helper没有添加第4个答案

时间:2017-07-05 04:27:58

标签: android database sqlite

我有一个简单的测验应用程序可以使用3个可能的答案而不是4个。该应用程序崩溃时出现以下错误: java.lang.IllegalStateException:无法从CursorWindow读取第0行,第6行。

引用游标的唯一代码是:

public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();

    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase = this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);

    while (cursor.moveToNext()) {
        Question quest = new Question();
        quest.setID(cursor.getInt(0));
        quest.setQUESTION(cursor.getString(1));
        quest.setANSWER(cursor.getString(2));
        quest.setOPTA(cursor.getString(3));
        quest.setOPTB(cursor.getString(4));
        quest.setOPTC(cursor.getString(5));
        quest.setOPTD(cursor.getString(6));
        quesList.add(quest);
    }

    return quesList;
}

这与另一个关于如何设置android数据库的一般问题不同。在这里,我试图找出有关CursorWindow的特定错误。

数据库表(DbHelper类)的函数在这里:

@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);

    onCreate(db);
}

Context context;
private static final int DATABASE_VERSION = 13;

private static final String DATABASE_NAME = "mathsone";
private static final String TABLE_QUEST = "quest";
private static final String KEY_ID = "qid";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer";
private static final String KEY_OPTA = "opta";
private static final String KEY_OPTB = "optb";
private static final String KEY_OPTC = "optc";
private static final String KEY_OPTD = "optd";

private SQLiteDatabase dbase;

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    dbase = db;
    String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
            + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
            + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT, " + KEY_OPTD + " TEXT)";
    db.execSQL(sql);
    addQuestion();
}

0 个答案:

没有答案