Search code examples
androidsqliteillegalstateexception

java.lang.IllegalStateException: Couldn't read row 1, col 4 from CursorWindow


I'm trying to get values from SQLite Database and adding them to an ArrayList. I don't know what the problem is. I can post more code if there is no problem in these code parts.

@Override
public void onCreate(SQLiteDatabase db) {

    String sql ="create table tablo(id integer primary key,tipi text,turu text, miktar text, notlar text ) ";
    db.execSQL(sql);



 public List<String> listAll(){


    List<String>liste = new ArrayList<String>();
    Cursor c = db.rawQuery("select * from tablo",null);
    c.moveToFirst();

    while (c.moveToNext()){
        int id = c.getInt(0);
        String tipi=c.getString(1);
        String turu = c.getString(2);
        String miktar = c.getString(3);
        String notlar =c.getString(4);

        Data d = new Data(id,tipi,turu,miktar,notlar);
        String toSt =d.toString();
        liste.add(toSt);
    }

Solution

  • A likely cause is that you have changed the SQL that creates the table by adding the notlar column.

    However simply changing that line alone will not make the change as the database helper's onCreate method only runs when the database is created.

    Additionally in the listAll method the line c.moveToFirst(); is very likely not wanted, as it will skip the first row. Hence the message indicating the problem at row 1 instead of row 0.

    To fix the issues,

    1) Amend the code to remove c.moveToFirst();.

    2) Do one of the following :-

    • Delete the App's data.
    • Uninstall the App.

    3) Rerun the App.