有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java更新SQlite数据库中的图像

我正在尝试更新SQlite数据库中的图像,但它给了我一个错误:

 Caused by: 安卓.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 and getImage is working fine.


共 (1) 个答案

  1. # 1 楼答案

    由于您试图在字符串中连接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);
    }