无法从CursorWindow读取第0行,col -1?

时间:2014-04-23 17:41:53

标签: android sqlite

我使用sqlite数据库开发了android应用程序。但我得到了错误,我无法解决这个问题。

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

这是我的代码

try {

            db = openOrCreateDatabase("book_category_db1",MODE_PRIVATE, null);
            db.execSQL("CREATE TABLE IF NOT EXISTS book_details1(book_name VARCHAR,author_name VARCHAR,description VARCHAR,state VARCHAR,category VARCHAR);");
            c1 = db.rawQuery("SELECT * FROM book_details1", null);


             if (c1.moveToFirst()) {
                    do {

                    String book_name =c1.getString(c1.getColumnIndex("book_name"));
                    String author_name = c1.getString(c1.getColumnIndex("auther_name"));
                    String state = c1.getString(c1.getColumnIndex("state"));

                    HashMap<String, String> map = new HashMap<String, String>();            


                    map.put(TAG_BOOK_NAME, book_name);
                    map.put(TAG_AUTHER_NAME, author_name);
                    map.put(TAG_STATE, state);
                    contactList.add(map);


                    } while (c1.moveToNext());

             }


            db.close();

        }

        catch (Exception e) {


            Toast.makeText(getApplicationContext(), "Your book list is emplty."+ e.toString(), Toast.LENGTH_LONG).show();               
            db.close();


        }

有人可以帮我解决这个问题。我尝试尝试但无法找到解决方案。我正在使用Android sqlire原始查询。

1 个答案:

答案 0 :(得分:1)

你拼错了列名:

String author_name = c1.getString(c1.getColumnIndex("auther_name"));

应该是

String author_name = c1.getString(c1.getColumnIndex("author_name"));

(注意:“auth o r_name”,而不是“auth e r_name”)

由于auther_name不是有效的列名,getColumnIndex会返回-1,这会引导您看到您遇到的异常。