1

The database is created using a SQLiteOpenHelper:

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String SQL_CREATE_INVENTORY_TABLE = "CREATE TABLE " + InventoryEntry.TABLE_NAME + "("
            + InventoryEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + InventoryEntry.COLUMN_PRODUCT_IMAGE + " BLOB, "
            + InventoryEntry.COLUMN_PRODUCT_NAME + " TEXT NOT NULL, "
            + InventoryEntry.COLUMN_PRODUCT_PRICE + " REAL NOT NULL, "
            + InventoryEntry.COLUMN_PRODUCT_QUANTITY + " INTEGER, "
            + InventoryEntry.COLUMN_PRODUCT_DESCRIPTION + " TEXT);";
    sqLiteDatabase.execSQL(SQL_CREATE_INVENTORY_TABLE);
}

All the constants are defined in a contract, this is the COLUMN_PRODUCT_IMAGE one:

public static final String COLUMN_PRODUCT_IMAGE = "image";

In a CursorAdapter, I try to access the image column from the bindView method, after adding some random data to the database:

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    ImageView imageView = view.findViewById(R.id.product_image);
    byte [] bytes = cursor.getBlob(cursor.getColumnIndexOrThrow(InventoryEntry.COLUMN_PRODUCT_IMAGE));
    Bitmap bitmap = BitmapUtils.getBitmap(bytes);
    imageView.setImageBitmap(bitmap);
}

This produces an java.lang.IllegalArgumentException: column 'image' does not exist

I double checked the database using windows cmd, here's how it looks:

sqlite> select * from inventory;
_id         image       name        price       quantity    description
----------  ----------  ----------  ----------  ----------  -----------
1                       ff          5.0         5           fds

Column 'image' exists, so why the error?

ohbabd
  • 13
  • 4
  • Are you adding the column to the selection when you create the cursor? – MatPag Aug 07 '18 at 22:01
  • 1
    Possible duplicate of [how to store Image as blob in Sqlite & how to retrieve it?](https://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it) – Martin Zeitler Aug 07 '18 at 22:04
  • 1
    the `projection` used to obtain the `cursor` might lack column `image`. – Martin Zeitler Aug 07 '18 at 22:05
  • As you said, I forgot to add it to the projection when querying the database, I fixed it now and all works fine. Thank you sirs! – ohbabd Aug 07 '18 at 22:08

0 Answers0