2017-06-15 146 views
1

我試圖更新SQLite數據庫的圖像,但它給了我一個錯誤:更新的SQLite數據庫的圖像

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

在這條線:

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.

+0

**最差實踐**。不要用二進制數據膨脹你的數據庫。改爲存儲**路徑**。 –

+0

好的,但二進制數據將需要時,我將數據保存在服務器數據庫。 – neens

+0

不一定。只需返回**網址**(相當於一個路徑)。 –

回答

0

既然你想在一個字符串來連接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); 
} 
+0

感謝兄弟。它的工作現在。你救了我的一天。 – neens