ERROR: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is correctly initialized before accessing data

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 09:28:18

问题


I have created a very simple database android app. It takes an input and display result. Add button is used to add the input and delete button for deleting the input stored in SQLite database. My cursor in Android SQLite is pointed correctly but still I am facing the error: Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. Following is the cursor part: Cursor c = db.rawQuery(query, null);

    c.moveToFirst();

    //Traversing through DB
    while (!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("studentname")) !=null)
        {
            dbString += c.getString(c.getColumnIndex("studentname"));
            dbString +="\n"; }

        c.moveToNext(); }
        db.close();

        return dbString;

I have tried changing cursor positions but still same error appearing. I have researched on other sites and same is the concept but still there is an issue.


回答1:


Apparently, there is no studentname column in your result set, if c.getColumnIndex("studentname") is returning -1.




回答2:


As mentioned above by CommonsWare, this apparently look like that there is no column of such name and its absolutely true. My column name is StudentName, not studentname, hence there was a mistake with respect to the proper column name, which resulted in to -1 (No column at all to work on). Thanks CommonsWare for pointing out my silly mistake :)

For those who want to start SQLite understanding with android, can check this course which I am also a part of and learning from it currently : https://www.udemy.com/androidcourse/ available at Udemy



来源:https://stackoverflow.com/questions/38819232/error-couldnt-read-row-0-col-1-from-cursorwindow-make-sure-the-cursor-is-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!