1

I have written the following code for inserting video bytes into the database
DBAdapter.java

public void insertVideoBytesInVideoDownloadsTable(int id, byte[] videoBytes){
    ContentDBHelper dbHelper = new ContentDBHelper(context);
    sqliteDB = dbHelper.getWritableDatabase();
    String sqlInsertVideoBytes = "Insert into " + tbl_video_downloads + " values (?, ?)";
    Log.i(TAG, sqlInsertVideoBytes);
    SQLiteStatement insertStatement = sqliteDB.compileStatement(sqlInsertVideoBytes);
    insertStatement.clearBindings();
    insertStatement.bindLong(1, id);
    Log.i(TAG, "Inserted: "+id);
    insertStatement.bindBlob(2, videoBytes);
    Log.i(TAG, "Inserted: "+videoBytes);
    insertStatement.executeInsert();
    Log.i(TAG, "Execute inserted");
   }

StoreVideoInDB.java

   public String downloadAndStoreVideoInDB(String urlPath){
        try {
            Log.i(TAG , "URL " +urlPath);
            URL url = new URL(urlPath);
            URLConnection connection = url.openConnection();
            connection.connect();
            InputStream videoStream = connection.getInputStream();
            BufferedInputStream videoBufferedStream = new BufferedInputStream(videoStream,128);
            ByteArrayBuffer videoByteArray = new ByteArrayBuffer(500);

            //Get the bytes one by one
            int current = 0;
            while((current = videoBufferedStream.read())!= -1){
                videoByteArray.append((byte)current);
                //Log.i(TAG, String.valueOf(current));
            }
            dbAdapter.insertVideoBytesInVideoDownloadsTable(id, videoByteArray.toByteArray());
        }catch (IOException ex) {
            Log.e(TAG, "error: " + ex.getMessage(), ex);
            ex.printStackTrace();
        }
        return path;
 }

DDMS Log:

01-16 16:03:13.563: INFO/DronaDBAdapter(18235): Insert into video_downloads values (?, ?)
01-16 16:03:13.563: INFO/DBAdapter(18235): Inserted: 1
01-16 16:03:13.573: INFO/DBAdapter(18235): Inserted: [B@44f35120
01-16 16:03:13.653: INFO/DBAdapter(18235): Execute inserted

As per the DDMS, the values 1(ID) and [B@44f35120 (Video Blob) values are showing successfully. But they are not inserted in DB.

When I check the DB and query as follows:

select * from video_downloads;

Result:

 1 | 

The second field is blank! No blob value is inserted! Why? Is there any thing wrong in my code

1
  • perhaps, convert your videoBytes into base64 and then save in database Commented Jan 16, 2012 at 11:29

1 Answer 1

1

I think you are misreading the output of the select. Your data is there, but the "select" does not show it, as it does not know what to do with the blob. Output its bytes literally makes no sense for most kinds of blobs.

Try reading the data back from your code and it will be there.

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

2 Comments

but whenever i checked in my prev projects on querying it showed blob data. This time it is not showing.
thanks. i got to see the data. i opened the database in Sqlite browser

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.