我正在尝试更新 SQlite 数据库中的图像,但它给了我一个错误:
Caused by: android.database.sqlite.SQLiteException: unrecognized token: "[B@d3d70b2" (code 1): , while compiling: UPDATE img set image=[B@d3d70b2
在这一行:
sdb.execSQL("UPDATE img set image=" + bytes + " WHERE id='" + i + "'");
我查了很多网站,但得到的只是如何插入图片。
这些是我用来插入、获取和更新图像的 3 个查询。
public void insertImage(byte[] bytes, int user){
ContentValues cv = new ContentValues();
cv.put("image", bytes);
cv.put("id", user);
int a= (int)sdb.insert("img", null, cv);
Toast.makeText(context,"inserted at "+a,Toast.LENGTH_SHORT).show();
}
public byte[] getimage(int i){
Cursor c1 = sdb.rawQuery("select image from img where id='" + i + "'", null);
c1.moveToNext();
byte[] b= c1.getBlob(0);
return b;
}
public void update(byte[] bytes, int i) {
sdb.execSQL("UPDATE img set image=" + bytes + " WHERE id='" + i + "'");
}
注意:insertImage 和 getImage 工作正常。
最佳答案
由于您尝试在字符串中连接 byte[]
,该代码基本上会尝试插入 bytes.toString()
而不是 bytes
。
试试这个:
public void update(byte[] bytes, int i) {
ContentValues cv = new ContentValues();
cv.put("image", bytes);
sdb.update("img", cv, "id=" + i, null);
}
关于java - 更新 SQlite 数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563446/