1

I'm trying to store and array of bytes (byte[]) into a DB. I have the array of bytes, a content provider and a database. When I try to insert into the Blob the insert returns -1. Can somebody show me how to get this done.

Insert

byte[] inData = nw.receive();
ContentValues values = new ContentValues();
values.put(InDatabase.Columns.DATA, inData);
values.put(InDatabase.Columns.SIZE, inData.length);
NetService.this.getContentResolver().insert(InContentProvider.CONTENT_URI, values);

Content Provider

mDB = mDBHelper.getWritableDatabase();
Long rowId = mDB.insert(mDBHelper.tableName(), null, aValues);

Database

@Override 
public void onCreate(SQLiteDatabase db) { 
        db.execSQL("CREATE table " + TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT, data BLOB, size INTEGER);"); 
}

Like I said If I don't put the values.put(InDatabase.Columns.DATA, inData), it works perfect.

0

1 Answer 1

0

So the implementation was right, the problem was with the SQLiteDatabaseHelper. I did not implement the onUpdate() method. Because my database already existed on the device with a different layout (the BLOB field did not exist) the insert method failed.

A quick uninstall of the .apk fixed the problem. I could also implement the onUpdate method so when the database version changes there is a table drop or update.

Sign up to request clarification or add additional context in comments.

1 Comment

The other thing you could do is increment the database version you are passing to your DbHelper constructor so that Android will automatically call the onUpgrade() function of your helper. You can just drop your DB tables and call onCreate() again from there to automatically recreate everything. Just another option you have that makes development a bit easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.