【问题标题】:Couldn't read row 0, col 8 from CursorWindow无法从 CursorWindow 读取第 0 行第 8 列
【发布时间】:2020-06-13 15:39:30
【问题描述】:

我有一个问题。我无法从我的数据库中获取和设置 blob。打开我的应用程序后,我的 Logcat 中出现此错误

原因:java.lang.IllegalStateException:无法读取第 0 行,第 8 列 从光标窗口。确保光标已正确初始化 在从中访问数据之前。

private static final String KEY_IMAGE = "image";

    public Note getNote(long id){
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.query(DATABASE_TABLE,new String[] {KEY_ID,KEY_TITLE,KEY_CONTENT,KEY_PHONE,KEY_CLIENT,KEY_AGE,KEY_DATE,KEY_TIME,KEY_IMAGE}, KEY_ID+"=?",

                    new String[]{String.valueOf(id)}, null, null,null);
            if (cursor != null)
                cursor.moveToFirst();
            return new Note(cursor.getLong(0)
                    ,cursor.getString(1)
                    ,cursor.getString(2)
                    ,cursor.getString(3)
                    ,cursor.getString(4)
                    ,cursor.getString(5)
                    ,cursor.getString(6)
                    ,cursor.getString(7)
            ,cursor.getBlob(8));
        }

        public List<Note> getNotes() {
            SQLiteDatabase db = this.getReadableDatabase();
            List<Note> allNotes = new ArrayList<>();

            String query = "SELECT * FROM " + DATABASE_TABLE + " ORDER BY "+KEY_TITLE+" ASC";
            Cursor cursor = db.rawQuery(query,null);
            if(cursor.moveToFirst()){
                do{
                    Note note = new Note();
                    note.setID(Long.parseLong(cursor.getString(0)));
                    note.setTitle(cursor.getString(1));
                    note.setAge(cursor.getString(2));
                    note.setPhone(cursor.getString(3));
                    note.setClient(cursor.getString(4));
                    note.setContent(cursor.getString(5));
                    note.setDate(cursor.getString(6));
                    note.setTime(cursor.getString(7));
                    note.setImage(cursor.getBlob(8));
                    allNotes.add(note);


                }while(cursor.moveToNext());

            }

            return allNotes;

        }

感谢您的帮助。

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    我认为您没有使用正确的方法来处理您在数据库中访问的数据, 例如,您说的是 getLong(0),但该列中可能存在其他类型的值,请仔细检查您的创建表查询,如果您想存储图像,则应使用文件提供程序而不是 blob。

    【讨论】:

    • 无济于事;/在公共注getNote游标KEY_IMAGE实现为new String,不知道byte[]怎么写,看看
    • 能否分享您的 SQLiteOpenHelper 子类
    • 我要的是你定义数据库表的类
    • 我没有看到你创建数据库的类
    • anotepad.com/notes/c483dixb 现在你明白了吗?"Zdjęcie" - 图片
    猜你喜欢
    • 2017-02-13
    • 2017-05-22
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 2016-11-29
    相关资源
    最近更新 更多