代码之家  ›  专栏  ›  技术社区  ›  Rohit Chauhan

如何在Sqlite Android中更新Blob数据类型

  •  0
  • Rohit Chauhan  · 技术社区  · 6 年前

    朋友我想更新列为NULL的Blob数据字段 每当我尝试以下代码时,它都会生成错误 我只是想知道 如何更新Blob数据类型??

            Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.audiobook);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            icon.compress(Bitmap.CompressFormat.PNG,100,stream);
            simple = stream.toByteArray();
            String sql  = "UPDATE Settings SET image = "+simple+"where image IS NULL";
            db.execSQL(sql);
    

    当我尝试此代码时,它会生成以下错误

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blackhat.rhythmbox/com.blackhat.rhythmbox.Welcome}: android.database.sqlite.SQLiteException: unrecognized token: ":" (code 1): , while compiling: UPDATE Settings SET image = :[B@27a97db7
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
     at android.app.ActivityThread.access$800(ActivityThread.java:178)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
     at android.os.Handler.dispatchMessage(Handler.java:111)
     at android.os.Looper.loop(Looper.java:194)
     at android.app.ActivityThread.main(ActivityThread.java:5637)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Caused by: android.database.sqlite.SQLiteException: unrecognized token: ":" (code 1): , while compiling: UPDATE Settings SET image = :[B@27a97db7
     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1704)
     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1635)
     at com.blackhat.rhythmbox.Welcome.addtoDatabase(Welcome.java:124)
     at com.blackhat.rhythmbox.Welcome.code(Welcome.java:67)
     at com.blackhat.rhythmbox.Welcome.onCreate(Welcome.java:57)
     at android.app.Activity.performCreate(Activity.java:6100)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601) 
     at android.app.ActivityThread.access$800(ActivityThread.java:178) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
     at android.os.Handler.dispatchMessage(Handler.java:111) 
     at android.os.Looper.loop(Looper.java:194) 
     at android.app.ActivityThread.main(ActivityThread.java:5637) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
    

    我哪里做错了,或者我做错了什么???

    2 回复  |  直到 6 年前
        1
  •  1
  •   Suraj Vaishnav    6 年前

    在之前添加空格 where 这样地:

    String sql  = "UPDATE Settings SET image = "+simple+" where image IS NULL";
    
        2
  •  0
  •   Paul Chu    6 年前

    删除:

    String sql  = "UPDATE Settings SET image = "+simple+"where image IS NULL";
    db.execSQL(sql);
    

    尝试使用 ContentValues.put() SqliteDatabase.update()

    ContentValues cv = new ContentValues();
    cv.put("image", simple);
    db.update("Settings", cv, "image is null", null);