Android SQLite奇怪的错误

时间:2017-03-16 12:15:57

标签: android sqlite android-sqlite

在这种情况下,有没有人遇到过这个错误?

无法从CursorWindow读取第1行第8行。

我知道这意味着它试图从第8列读取数据,但问题是我只有7列,而且我从不试图从第8列分配数据。

我遇到的问题是每当我清除数据并重新开始时,它运行正常,然后突然开始抛出错误。

这是我的表:

public String CREATE_TABLE = "CREATE TABLE BreakTests ( " +
        "Id INTEGER PRIMARY KEY AUTOINCREMENT, " +
        "Answered_Question_Set_ID INTEGER, " +
        "PassValue INTEGER, " +
        "Value1 INTEGER, " +
        "Value2 INTEGER, " +
        "Value3 INTEGER, " +
        "Value4 INTEGER, " +
        "Value5 INTEGER)";

这些是我的专栏:

public final String KEY_ID = "Id";
public final String KEY_ANSWERED_QUESTION_SET_ID = "Answered_Question_Set_ID";
public final String KEY_PASS_VALUE = "PassValue";
public final String KEY_VALUE_1 = "Value1";
public final String KEY_VALUE_2 = "Value2";
public final String KEY_VALUE_3 = "Value3";
public final String KEY_VALUE_4 = "Value4";
public final String KEY_VALUE_5 = "Value5";

public final String[] COLUMNS = {KEY_ID, KEY_ANSWERED_QUESTION_SET_ID, KEY_PASS_VALUE, KEY_VALUE_1, KEY_VALUE_2, KEY_VALUE_3, KEY_VALUE_4, KEY_VALUE_5};

这是我的疑问:

 public ArrayList<BreakTest> GetForSync() {
    ArrayList<BreakTest> tests = new ArrayList<BreakTest>();

    String selectQuery = "SELECT Id, Answered_Question_Set_ID, PassValue, Value1, Value2, Value3, Value4, Value5 FROM BreakTests";
    Cursor cursor = Shared.db.getReadableDatabase().rawQuery(selectQuery, new String[] {  });
    BreakTest breakTest;
    if (cursor.moveToFirst()) {
        do {
            breakTest = new BreakTest();
            breakTest.Id = cursor.getInt(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
            breakTest.Answered_Question_Set_ID = cursor.getInt(cursor.getColumnIndex(KEY_ANSWERED_QUESTION_SET_ID));
            breakTest.PassValue = cursor.getInt(cursor.getColumnIndex(KEY_PASS_VALUE));
            breakTest.Value1 = cursor.getInt(cursor.getColumnIndex(KEY_VALUE_1));
            breakTest.Value2 = cursor.getInt(cursor.getColumnIndex(KEY_VALUE_2));
            breakTest.Value3 = cursor.getInt(cursor.getColumnIndex(KEY_VALUE_3));
            breakTest.Value4 = cursor.getInt(cursor.getColumnIndex(KEY_VALUE_4));
            breakTest.Value5 = cursor.getInt(cursor.getColumnIndex(KEY_VALUE_5));

            tests.add(breakTest);
        } while (cursor.moveToNext());
    }

    return tests;
}

我已经与Android Studio和SQLite合作了一段时间,而且我从未遇到过这个问题。

非常感谢任何意见/反馈!

3 个答案:

答案 0 :(得分:1)

感谢所有评论,问题在于这行代码;

breakTest.Id = cursor.getInt(cursor.getInt(cursor.getColumnIndex(KEY_ID)));

已更改为

breakTest.Id = cursor.getInt(cursor.getColumnIndex(KEY_ID));

答案 1 :(得分:0)

为什么在不需要传递参数时提供new String[] {}

试试这个

Cursor cursor = Shared.db.getReadableDatabase().rawQuery(selectQuery, null);

答案 2 :(得分:-1)

据我所知,你的表 8列,而不是7列。 Id,问题,传递和5个值。 因此,消息&#34;无法从第8列和第34列读取;如果它是非常有效的,例如只是NULL,你尝试.getIntUse cursor.isNull(columnIndex)要确保内部有有效数据,例如

if (!cursor.isNull(cursor.getColumnIndex(KEY_VALUE_1))) breakTest.value1=cursor.getInt(...)

您的所有列都未定义为NOT NULL,因此您需要签入代码。