使用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);
}
所有常量都在合同中定义,这就是COLUMN_PRODUCT_IMAGE
:
public static final String COLUMN_PRODUCT_IMAGE = "image";
在CursorAdapter中,将一些随机数据添加到数据库后,我尝试通过image
方法访问bindView
列:
@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);
}
这会产生一个java.lang.IllegalArgumentException: column 'image' does not exist
我使用Windows cmd再次检查了数据库,如下所示:
sqlite> select * from inventory;
_id image name price quantity description
---------- ---------- ---------- ---------- ---------- -----------
1 ff 5.0 5 fds
存在“图像”列,为什么会出错?