2012-01-16 1 views
1

にSQLiteのデータベースに格納されていないなぜビデオを挿入するための次のコードを書かれているデータベース
にバイトDBAdapter.javaBLOB値をアンドロイド

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ログ:

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: [[email protected] 
01-16 16:03:13.653: INFO/DBAdapter(18235): Execute inserted 

DDMSごとに、値1(ID)と[B44f35120(ビデオBLOB)]の値が正常に表示されています。しかし、DBには挿入されません。

次のように私はDBと、クエリを確認してください。

select * from video_downloads; 

結果:

1 | 

2番目のフィールドが空白になっているの! BLOB値は挿入されません!どうして?私のコードに何か間違っていますか?

+0

おそらくvideoBytesをbase64に変換し、データベース – waqaslam

+0

に保存してください。私はそれを試してみます。 –

答えて

1

私はあなたが選択の出力を誤解していると思います。あなたのデータはそこにありますが、 "選択"にはそれが表示されません。なぜなら、ブロブで何をするのかわからないからです。そのバイトを出力することは文字通り大部分のブロブに意味をなさない。

コードからデータを読み取ってみると、そこに表示されます。

+0

しかし、質問に私の前のプロジェクトをチェックインするたびにそれはブロブのデータを示した。今回は表示されません。ありがとう。 –

+0

ありがとう。私はそのデータを見なければならない。私はSqliteブラウザでデータベースを開いた –