gpt4 book ai didi

java - 无法从 CursorWindow 读取第 0 行第 8 列

转载 作者:行者123 更新时间:2023-12-01 18:10:51 25 4
gpt4 key购买 nike

我遇到了问题。我无法从数据库获取和设置 blob。打开我的应用程序后,我在 Logcat 中收到此错误

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

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;

}

感谢您的帮助。

最佳答案

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

关于java - 无法从 CursorWindow 读取第 0 行第 8 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60470267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com