android - 为什么 blob 值不存储在 android 中的 sqlite 数据库中

标签 android sqlite

我编写了以下代码用于将视频字节插入数据库
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 (Video Blob) 值已成功显示。但是它们没有插入到数据库中。

当我检查数据库并查询如下时:

select * from video_downloads;

结果:

 1 | 

第二个字段是空白的!没有插入 blob 值!为什么?我的代码有什么问题吗

最佳答案

我认为您误读了选择的输出。您的数据在那里,但“选择”不显示它,因为它不知道如何处理 blob。对于大多数类型的 blob,逐字输出其字节没有意义。

尝试从您的代码中读回数据,它会在那里。

关于android - 为什么 blob 值不存储在 android 中的 sqlite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8879272/

相关文章:

android - 操作栏颜色无论如何都不会改变?

java - 尝试运行引用库的精简版本时出现 ClassNotFoundException

java - 为什么我的 SQL 查询没有从 sqlite 数据库中选择任何行

android - 您可以在 Web 应用程序和原生 Android 应用程序之间共享任何类型的信息吗?

Android:单行中的EditText提示

java - 是否可以使用 Intent 将数据传递到不同的 Activity ,但不启动该特定 Activity ?

c++ - 我调用 sqlite_open_v2 有什么问题?

mysql - SQLITE 查询基于列的值检索列名

android - 在 Android 中保存多个图像

datetime - 在iPhone中的sqlite3数据库中读取日期(“DateTime”类型)