我正在尝试将byte[]
插入SQLite
数据库中,但出现NOT NULL constraint failed
错误。我在插入和byte[] toString() => [B@e9e5fac
和byte[] length=> 32
之前检查了值,因此肯定不是null。
这是我的数据库表定义:
public static final String TABLE_USER_BLOB_STORE = "user_blob_store";
public static final String USER_BLOB_ID = "_id";
public static final String USER_BLOB_USER_ID = "user_id";
public static final String USER_BLOB = "user_blob";
public static final String[] ALL_COLUMNS_USER_BLOB_STORE = {
USER_BLOB_ID, USER_BLOB_USER_ID, BLOB_STORE};
private static final String TABLE_CREATE_USER_BLOB_STORE =
"CREATE TABLE " + TABLE_USER_BLOB_STORE + " (" +
USER_BLOB_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
USER_BLOB_USER_ID + " INTEGER NOT NULL," +
USER_BLOB + " BLOB NOT NULL" +
")";
这是插入方法:
public void insertUserBlob(int userId, byte[] userBlob, Context context) {
Log.d("userBlob","string value => " + userBlob.toString()); // [B@e9e5fac
Log.d("userBlob","length => " + userBlob.length); // 32
ContentValues setValues = new ContentValues();
setValues.put(DBOpenHelper.USER_BLOB_USER_ID , userId);
setValues.put(DBOpenHelper.USER_BLOB , userBlob);
Uri setUri = context.getContentResolver().insert(MyProvider.CONTENT_URI_USER_BLOB, setValues);
}
答案 0 :(得分:1)
我可以用这种方法插入
public long createBLB(int userId, byte[] userBlob)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DIALOGID, userId);
values.put(USER_BLOB, userBlob);
long transaction = db.insert(TABLE_BLBS, null, values);
return transaction;
}
仅作为示例:
String string = "byte";
byte[] result = new byte[100];
System.arraycopy(string.getBytes(), 0, result, 100 - string.length(), string.length());
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
long t = db.createBLB(1, result);