0

我正在尝试将位图存储为 BLOB 和一些字符串在同一个 SQLdatase 中。

这是我的数据库:

// 更新单个联系人

public int update(String name, String email, String uid, String tel, String niver, String created_at, byte[] thumb) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name); // Name
    values.put(KEY_EMAIL, email); // Email
    values.put(KEY_UID, uid); // Email
    values.put(KEY_TEL, tel); // Telefone
    values.put(KEY_NIVER, niver); // Niver
    values.put(KEY_CREATED_AT, created_at); // Created At
    values.put(KEY_THUMB, thumb); // thumb

    //db.close(); // I deleted this line

    // updating row
    return db.update(TABLE_LOGIN, values, KEY_THUMB + " = ?",
            new String[] { String.valueOf(getRowCount()) });
}

我用它来用位图更新数据库;

                    imagebutton.setImageBitmap(photo);
                    imagebutton.setScaleType(ScaleType.FIT_XY);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte[] imageInByte = stream.toByteArray();
                    db.update(null, null, null, null, null, null, imageInByte);

日志:

01-21 15:47:47.445: E/AndroidRuntime(24673): FATAL EXCEPTION: main
01-21 15:47:47.445: E/AndroidRuntime(24673): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=-1, data=Intent { act=inline-data (has extras) }} to activity {br.com.example.nightid/br.com.example.nightid.main.BaseSampleActivity}: java.lang.IllegalStateException: database not open
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.os.Looper.loop(Looper.java:130)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at java.lang.reflect.Method.invokeNative(Native Method)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at java.lang.reflect.Method.invoke(Method.java:507)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at dalvik.system.NativeStart.main(Native Method)
01-21 15:47:47.445: E/AndroidRuntime(24673): Caused by: java.lang.IllegalStateException: database not open
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1712)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1666)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at com.example.androidhive.library.DatabaseHandler.update(DatabaseHandler.java:150)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at br.com.example.nightid.slidingmenu.SlidingFragment.onActivityResult(SlidingFragment.java:204)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:161)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.app.Activity.dispatchActivityResult(Activity.java:3919)
01-21 15:47:47.445: E/AndroidRuntime(24673):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
01-21 15:47:47.445: E/AndroidRuntime(24673):    ... 11 more
4

1 回答 1

0

使用此代码,它现在可以工作:

 public int updateUser(byte[] thumb) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_THUMB, thumb); // thumb
        Log.w(TAG, String.valueOf(thumb));
        // updating row
        return db.update(TABLE_LOGIN, values, null,
                null );
    }

new String[] { String.valueOf(getRowCount()) } 在 logcat 上给了我这个错误。

于 2013-01-21T21:03:56.747 回答