【问题标题】:Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it无法从 CursorWindow 读取第 0 行第 3 列。确保在从光标访问数据之前正确初始化光标
【发布时间】:2015-02-25 15:59:13
【问题描述】:

我在我的 sqlite 数据库中插入图像时遇到了困难。我的代码中没有语法错误。运行后,应用程序会自动强制停止。 logcat 显示:

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

at com.synergy88studios.catalogapp.DatabaseHandler.getAllItemsInList(DatabaseHandler.java:86)

at com.synergy88studios.catalogapp.MainActivity.onCreate(MainActivity.java:56)

这是我在 DatabaseHandler.class 中的 getAllItemsInList 中的方法

public List<Item> getAllItemsInList() {
    List<Item> itemList = new ArrayList<Item>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_ITEMS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Item item = new Item();
            item.set_id(Integer.parseInt(cursor.getString(0)));
            item.set_name(cursor.getString(1));
            item.set_description(cursor.getString(2));
            item.set_image(cursor.getBlob(3));
            // Adding contact to list
            itemList.add(item);
        } while (cursor.moveToNext());
    }

    // return item list
    return itemList;
}

在我的 MainActivity 中,我只是调用该方法。

items = db.getAllItemsInList();

编辑

DatabaseHandler.class

public class DatabaseHandler extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "CatalogItems";
public static final String TABLE_ITEMS = "Items";

public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_DESC = "description";
public static final String KEY_IMAGE = "image";
public static final String[] ALL_KEYS = new String[] {KEY_ID, KEY_NAME, KEY_DESC};
//private static final String KEY_IMAGE = "image";

public DatabaseHandler(Context context){
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase db){
    String CREATE_ITEMS_TABLE = "CREATE TABLE " + TABLE_ITEMS + "( "
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_DESC + " TEXT, " + KEY_IMAGE + " BLOB" + ")";
    db.execSQL(CREATE_ITEMS_TABLE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL("DROP TABLE IF EXISTS "+ TABLE_ITEMS);
    onCreate(db);
}

public void addItems(Item item){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, item.get_name());
    values.put(KEY_DESC, item.get_description());
    values.put(KEY_IMAGE, item.get_image());

    db.insert(TABLE_ITEMS, null ,values);
    db.close();
}

Item getItem(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_ITEMS, new String[] { KEY_ID,
                    KEY_NAME, KEY_DESC , KEY_IMAGE}, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    Item item = new Item(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2),  cursor.getBlob(3));

    return item;
}

void deleteAllItems() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DELETE FROM "+ TABLE_ITEMS);

}

public List<Item> getAllItemsInList() {
    List<Item> itemList = new ArrayList<Item>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_ITEMS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Item item = new Item();
            item.set_id(Integer.parseInt(cursor.getString(0)));
            item.set_name(cursor.getString(1));
            item.set_description(cursor.getString(2));
            item.set_image(cursor.getBlob(3));
            // Adding contact to list
            itemList.add(item);
        } while (cursor.moveToNext());
    }

    // return item list
    return itemList;
}

public Cursor getAllRows() {
    SQLiteDatabase db = this.getWritableDatabase();
    String where = null;
    Cursor c =  db.query(true, TABLE_ITEMS, ALL_KEYS,
            where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}


public int getItemsCount(){
    String countQuery = "SELECT * FROM " + TABLE_ITEMS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);

    return cursor.getCount();

}

public int updateItem(Item item) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, item.get_name());
    values.put(KEY_DESC, item.get_description());
    values.put(KEY_IMAGE, item.get_image());

    // updating row
    return db.update(TABLE_ITEMS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(item.get_id()) });
}

public void deleteContact(Item item) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_ITEMS, KEY_ID + " = ?",
            new String[] { String.valueOf(item.get_id()) });
    db.close();
 }

}

我希望有人可以向我解释发生了什么。我可以发布我的其他课程以供参考。

【问题讨论】:

标签: java android database sqlite cursor


【解决方案1】:

不要在 Android sqlite 表中存储大量数据。特别是,CursorWindow 仅支持最大 2MB 的行数据。如果您的行较大,则无法访问它。

相反,将您的 blob 作为文件存储在文件系统中,并仅将路径存储在数据库中。

【讨论】:

  • 嗨@laalto,你能提供任何关于如何实现它的教程吗?谢谢
猜你喜欢
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多