如何使用SQLite修复CursorIndexOutOfBoundsException并检索数据

时间:2018-04-29 15:41:33

标签: android sqlite cursor

我知道这个问题可能会被多次询问。这是我第一次在Android中使用SQLite。我的问题是,我想在点击按钮时保存一个对象。我在github上搜索并阅读了有关SQLite的文章。直到现在我可以保存此object但我无法检索它,因为我在使用CursorIndexOutOfBoundsException函数时得到getSkip。除此之外,我不知道我的代码是否正确。

这是我的班级

public class SkippedQuestions {
public static final String TABLE_NAME = "skiptable";
public static final String ID = "id";
public static final String QUESTION = "question";
public static final String ANSONE = "answerone";
public static final String ANSTWO = "answertwo";
public static final String ANSTHREE = "answerthree";
public static final String ANSFOUR = "answerfour";

private int id;
private String question,ans1,ans2,ans3,ans4;

public static final String CREATE_TABLE =
        "CREATE TABLE "+TABLE_NAME+"("+
                ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
        +QUESTION+" TEXT,"
        + ANSONE +" TEXT,"
        + ANSTWO +" TEXT,"
        + ANSTHREE +" TEXT,"
        + ANSFOUR +" TEXT)";

public static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + TABLE_NAME;

public SkippedQuestions() {
}


public SkippedQuestions(int id,String question,String ans1,String ans2,String ans3,String ans4) {
    this.id=id;
    this.question = question;
    this.ans1=ans1;
    this.ans2=ans2;
    this.ans3=ans3;
    this.ans4=ans4;
}

//Then my setter and getter

我的Database班级

public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "skip_db";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
        db.execSQL(SkippedQuestions.CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SkippedQuestions.SQL_DELETE_ENTRIES);
    onCreate(db);
}

public boolean insertSkip(String quest,String ans1,String ans2,String ans3,String ans4) {
    // get writable database as we want to write data
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(SkippedQuestions.QUESTION,quest);
    values.put(SkippedQuestions.ANSONE,ans1);
    values.put(SkippedQuestions.ANSTWO,ans2);
    values.put(SkippedQuestions.ANSTHREE,ans3);
    values.put(SkippedQuestions.ANSFOUR,ans4);
    //insert row
     db.insert(SkippedQuestions.TABLE_NAME,null,values);
    return true;
}



public Cursor getSkip(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery("SELECT * FROM " + SkippedQuestions.TABLE_NAME + " WHERE " +
            SkippedQuestions.ID + "=?", new String[]{Integer.toString(id)});
    return res;
}


public ArrayList<SkippedQuestions> getAllSkipped() {
    ArrayList<SkippedQuestions> skippes = new ArrayList<>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + SkippedQuestions.TABLE_NAME + " ORDER BY " +
            SkippedQuestions.ID + " DESC";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            SkippedQuestions skippedQuestions = new SkippedQuestions();
            skippedQuestions.setId(cursor.getInt(cursor.getColumnIndex(SkippedQuestions.ID)));
            skippedQuestions.setQuestion(cursor.getString(cursor.getColumnIndex(SkippedQuestions.QUESTION)));
            skippedQuestions.setAns1(cursor.getString(cursor.getColumnIndex(SkippedQuestions.ANSONE)));
            skippedQuestions.setAns2(cursor.getString(cursor.getColumnIndex(SkippedQuestions.ANSTWO)));
            skippedQuestions.setAns3(cursor.getString(cursor.getColumnIndex(SkippedQuestions.ANSTHREE)));
            skippedQuestions.setAns4(cursor.getString(cursor.getColumnIndex(SkippedQuestions.ANSFOUR)));
            skippes.add(skippedQuestions);
        } while (cursor.moveToNext());
    }

    // close db connection
    db.close();

    // return notes list
    return skippes;
}

以下是exception发生的

i此处代表我ArrayList i`的object'. And I tried to put 0 instead of索引,我得到同样的例外

Cursor rs = db.getSkip(i);
 String quest = rs.getString(rs.getColumnIndex(SkippedQuestions.QUESTION));
 if (!rs.isClosed()) {
   rs.close();
 }

1 个答案:

答案 0 :(得分:1)

在这些行之间添加if (rs.moveToFirst())条件:

Cursor rs = db.getSkip(i);
String quest = rs.getString(rs.getColumnIndex(SkippedQuestions.QUESTION));

首先,Cursor指向行号-1,它不是有效行。试图从中读取会导致异常。如果移动成功,则游标moveTo...()方法返回true。如果没有行,则返回false。