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

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

我创建了一个非常简单的数据库 android 应用程序。它接受输入并显示结果。添加按钮用于添加输入,删除按钮用于删除存储在 SQLite 数据库中的输入。我在 Android SQLite 中的 cursor 指向正确,但我仍然面临错误: Caused by: java.lang.IllegalStateException: Couldn't read row 0 ,来自 CursorWindow 的 col -1。在从游标访问数据之前,请确保游标已正确初始化。 以下是游标部分: 游标 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;

我试过更改光标位置,但仍然出现同样的错误。我在其他网站上进行了研究,概念相同,但仍然存在问题。

显然,如果 c.getColumnIndex("studentname") 返回 -1,那么您的结果集中没有 studentname 列。

正如 CommonsWare 上面提到的,这显然看起来没有这样名称的列,而且它是绝对正确的。我的列名称是 StudentName,而不是 studentname,因此在正确的列名称方面存在错误,导致 -1(根本没有要处理的列)。感谢 CommonsWare 指出我的愚蠢错误:)

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