[英]C++ copy sqlite blob from one database to another
I am trying to copy some blob data from one sqlite table to another in C++. 我试图在C ++中将一些blob数据从一个sqlite表复制到另一个sqlite表。 However, once the data has been copied over to the new table, it seems to be getting corrupted.
但是,一旦将数据复制到新表,它似乎就会被破坏。 The said data contains some jpeg images.
所述数据包含一些jpeg图像。 The code I am using to copy from TABLE1 to TABLE2 is shown below:
我用来从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 );
Is there anything I am doing wrong in the above code, the reason I say that it is getting corrupted is because, I am reading the blobs on an android device to be displayed in an image viewer. 在上面的代码中有什么我做错了,我说它被破坏的原因是因为,我正在读取Android设备上的blob以显示在图像查看器中。 The blobs in the TABLE 1 can be read and displayed fine, however the ones in TABLE 2 do not display anything.
可以读取和显示表1中的blob,但是表2中的blob不显示任何内容。 Any help is greatly appreaciated.
任何帮助都非常有用。
SOLUTION: 解:
// 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 );
sqlite3_bind_blob
expects a pointer to the actual blob data; sqlite3_bind_blob
需要一个指向实际blob数据的指针; it is not possible to use a blob handle for that. 它不可能使用blob句柄。
To get the blob data as a memory chunk, execute a query like SELECT tile_data FROM MyTable WHERE ...
and read the value with sqlite3_column_blob . 要将blob数据作为内存块,请执行
SELECT tile_data FROM MyTable WHERE ...
并使用sqlite3_column_blob读取该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.