【问题标题】:C++ copy sqlite blob from one database to anotherC++ 将 sqlite blob 从一个数据库复制到另一个数据库
【发布时间】:2014-03-12 04:18:50
【问题描述】:

我试图在 C++ 中将一些 blob 数据从一个 sqlite 表复制到另一个。但是,一旦将数据复制到新表中,它似乎就会损坏。所述数据包含一些jpeg图像。我用来从 TABLE1 复制到 TABLE2 的代码如下所示:

// Read the blob from the database
    int64_t rowID = 0;
    sscanf( id.c_str(), "%llu", &rowID );

    sqlite3_blob* blobHandle = NULL;
    if( sqlite3_blob_open( m_dbHandle_temp, "main", m_currentTileTable.c_str(), "tile_data", rowID, 0, &blobHandle ) != SQLITE_OK )
    {
        sqlite3_blob_close( blobHandle ); // An SQLite blob will be initialised regardless of the success of 'sqlite3_blob_open'
        return false;
    }


    tiles_insert_statement.append( ")" );

    // Copy blob to database
    sqlite3_stmt *stmt = 0;
    const char* tail;
    sqlite3_prepare_v2( m_dbHandle, tiles_insert_statement.c_str(), strlen( tiles_insert_statement.c_str() )+1, &stmt, &tail );
    int bindSuccess = sqlite3_bind_blob( stmt, 1, blobHandle, sqlite3_blob_bytes( blobHandle ), SQLITE_TRANSIENT );
    if( sqlite3_step( stmt ) != SQLITE_DONE ) 
        printf( "Error message: %s\n", sqlite3_errmsg( m_dbHandle ) );
    sqlite3_finalize( stmt );

    // close handles
    sqlite3_blob_close( blobHandle );

我在上面的代码中做错了什么吗,我说它被损坏的原因是因为,我正在读取 android 设备上的 blob 以显示在图像查看器中。表 1 中的 blob 可以正常读取和显示,但表 2 中的 blob 不显示任何内容。非常感谢任何帮助。

解决方案:

// Read the blob from the database
    int64_t rowID = 0;
    sscanf( id.c_str(), "%llu", &rowID );

    sqlite3_blob* blobHandle = NULL;
    if( sqlite3_blob_open( m_dbHandle_temp, "main", m_currentTileTable.c_str(), "tile_data", rowID, 0, &blobHandle ) != SQLITE_OK )
    {
        sqlite3_blob_close( blobHandle ); // An SQLite blob will be initialised regardless of the success of 'sqlite3_blob_open'
        return false;
    }

    unsigned int length = sqlite3_blob_bytes( blobHandle );

    // TODO - instances of this class OWN the buffer.
    // Delete the buffer in the destructor ;)
    unsigned char* buffer = new unsigned char[ length ];
    if( sqlite3_blob_read( blobHandle, buffer, length, 0 ) != SQLITE_OK )
    {
        return false;
    }

    tiles_insert_statement.append( ")" );

    sqlite3_stmt *stmt = 0;
    const char* tail;
    sqlite3_prepare_v2( m_dbHandle, tiles_insert_statement.c_str(), strlen( tiles_insert_statement.c_str() )+1, &stmt, &tail );
    int bindSuccess = sqlite3_bind_blob( stmt, 1, buffer, length, SQLITE_TRANSIENT );
    if( sqlite3_step( stmt ) != SQLITE_DONE ) 
        printf( "Error message: %s\n", sqlite3_errmsg( m_dbHandle ) );
    sqlite3_finalize( stmt );

    // close handles
    sqlite3_blob_close( blobHandle );

【问题讨论】:

  • 也许您最好将一个数据库附加到另一个数据库并在 sql 查询中执行 blob 复制?

标签: c++ sqlite blob


【解决方案1】:

sqlite3_bind_blob 需要一个指向实际 blob 数据的指针;不能为此使用 blob 句柄。

要将 blob 数据作为内存块获取,请执行类似 SELECT tile_data FROM MyTable WHERE ... 的查询并使用 sqlite3_column_blob 读取值。

【讨论】:

  • 我已经编辑了我的问题以显示我最终做了什么。在回到这篇文章之前,我想通了。正如您所建议的,问题是由于我已将 blob 句柄传递给 sqlite3_bind_blob
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
相关资源
最近更新 更多