我编写了以下用于将视频字节插入数据库的代码 的 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日志:
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
根据DDMS,值1(ID)和[B @ 44f35120(视频Blob)值显示成功。但它们不会插入数据库中。
当我检查数据库并查询时如下:
select * from video_downloads;
结果:
1 |
第二个字段是空白的!没有插入blob值!为什么?我的代码中有什么问题吗
答案 0 :(得分:1)
我认为你误读了select的输出。您的数据存在,但“选择”没有显示,因为它不知道如何处理blob。输出它的字节对于大多数类型的blob来说毫无意义。
尝试从代码中读回数据,它就在那里。