无法从CursorWindow读取第0行,第3列。在从中访问数据之前,请确保正确初始化Cursor

时间:2015-02-25 06:02:16

标签: java android database sqlite cursor

我在我的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();
 }

}

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

1 个答案:

答案 0 :(得分:2)

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

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