java - 更新 SQlite 数据库中的图像

标签 java android sqlite android-sqlite blob

我正在尝试更新 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/

相关文章:

java - jsch ssh 连接无法获取authorized_keys

java - 排除 JAXB 中的字段

java - 2 个互相搜索栏 (android)

java - 如何在知道部分名称的目录中查找文件

java - 在Android studio中使用.so(共享对象)文件

php - PDO准备语句来存储html内容

android - 替换AChartEngine饼图的数据

android - 如何提高网络提供商的位置准确性?安卓

javascript - 如何将java字符串插入数据库表?

python - 将列名作为变量传递给 python/pandas 函数中的 select 语句