简体   繁体   中英

update an image in SQlite Database

i am trying to update an image in SQlite database but it gives me an error:

 Caused by: android.database.sqlite.SQLiteException: unrecognized token: "[B@d3d70b2" (code 1): , while compiling: UPDATE img set image=[B@d3d70b2

on this line:

    sdb.execSQL("UPDATE img set image=" + bytes + " WHERE id='" + i + "'");

i have searched on many websites but what i got is only how to insert an image.

These are the 3 queries which i am using to insert, get and update the image.

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 + "'");
}

Note: insertImage and getImage is working fine.

Since you're trying to concatenate byte[] in a String, that code will basically try to insert bytes.toString() not bytes .

Try this instead:

public void update(byte[] bytes, int i) {
    ContentValues cv = new ContentValues();
    cv.put("image", bytes);
    sdb.update("img", cv, "id=" + i, null);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM