从android中的sqlite数据库通过游标读取数据时出现异常

时间:2013-07-08 10:47:10

标签: android sqlite

我的数据已成功添加到database.i已通过调试我的代码进行检查。但是当我使用游标读取此数据时,它会抛出以下异常:              java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.这是我的数据库帮助函数代码:

                                `///get schools
                    public ArrayList<SchoolModel> getSchools() {

                        school.clear();

                        SQLiteDatabase db = this.getReadableDatabase();
                        Cursor cursor = db.rawQuery("select * from School", null);
                        if ((cursor.getCount() >0)){
                            if (cursor.moveToFirst()) {
                                do {
                                    SchoolModel item = new SchoolModel();
                                    item.SchoolID = cursor.getInt(cursor
                                            .getColumnIndex("SchoolID "));
                                    item.Address= cursor.getString(cursor
                                            .getColumnIndex("Address"));
                                    item.Cell= cursor.getString(cursor
                                            .getColumnIndex("Cell"));
                                    item.ContactPerson= cursor.getString(cursor
                                            .getColumnIndex("ContactPerson"));
                                    item.Description= cursor.getString(cursor
                                            .getColumnIndex("Description"));
                                    item.Phone= cursor.getString(cursor
                                            .getColumnIndex("Phone"));
                                    item.AreaID = cursor.getInt(cursor
                                            .getColumnIndex("AreaID"));


                                    school.add(item);

                                } while (cursor.moveToNext());
                            }
                        }
                        cursor.close();
                        db.close();
                        return school;
                    }

有谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:1)

 item.SchoolID = cursor.getInt(cursor  .getColumnIndex("SchoolID "));

可能在首都D帮助后删除空格