SQLite Cursor初始化不正确

时间:2017-01-17 14:25:36

标签: android sqlite cursor android-sqlite

我收到此错误:

java.lang.IllegalStateException:无法从CursorWindow读取第0行,第28行。在从中访问数据之前,请确保Cursor已正确初始化。

这是我的数据库处理程序类中的光标......是否有任何可见的错误?

    // retrieve a single friend
public Friend getFriend(int id){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();

Friend friend = null;

if( checkIsDataAlreadyInDBorNot( TABLE_FRIENDS, KEY_ID, id ) )
{
    Cursor cursor = sqLiteDatabase.query(TABLE_FRIENDS, new String[] { 
            KEY_ID, KEY_GEN, KEY_FIRSTNAME, KEY_MIDDLENAME, 
            KEY_LASTNAME, KEY_BD, KEY_BM, KEY_BY, KEY_RN, KEY_HC,
            KEY_HT, KEY_NB, KEY_NS, KEY_SP, KEY_RS, KEY_CH, KEY_P,
            KEY_FC, KEY_FMU, KEY_LFM, KEY_FMO, KEY_LFMG, KEY_FTS,
            KEY_FAF, KEY_LFFS, KEY_HO, KEY_OC, KEY_PP}, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);

    if (cursor != null)
    {
        cursor.moveToFirst();
    }



    friend = new Friend(cursor.getInt(0),
            cursor.getString(1),
            cursor.getString(2),
            cursor.getString(3),
            cursor.getString(4),
            cursor.getInt(5),
            cursor.getInt(6),
            cursor.getInt(7),
            cursor.getInt(8),
            cursor.getString(9),
            cursor.getString(10),
            cursor.getString(11),
            cursor.getInt(12),
            cursor.getInt(13),
            cursor.getString(14),
            cursor.getString(15),
            cursor.getInt(16),
            cursor.getInt(17),
            cursor.getString(18),
            cursor.getString(19),
            cursor.getString(20),
            cursor.getString(21),
            cursor.getString(22),
            cursor.getString(23),
            cursor.getString(24),
            cursor.getString(25),
            cursor.getString(26),
            cursor.getString(27),
            cursor.getString(28));
}

return friend;
}

1 个答案:

答案 0 :(得分:2)

列索引从0开始,而不是从1开始。您的光标有28列,您正在尝试阅读第29列。

考虑使用getColumnIndex()代替(或者更好,getColumnIndexOrThrow())以按列名称获取列索引,而不是对索引进行硬编码。