1

我正在使用 MySql 连接器 C++ 将 JPEG 图像从文件存储到数据库中。我正在使用准备好的语句。执行准备好的语句后,仅将文件的前 64 个字节复制到数据库中。

我对示例的研究表明不需要迭代,并且示例假设准备好的语句加载整个文件。这是我的代码:

std::string         statement_text("INSERT INTO ");
statement_text += "picture_image_data";
statement_text += " (";
statement_text += "ID_Picture";
statement_text += ", ";
statement_text += "Image_Data";
statement_text += ") VALUES (?, ?)";    
wxLogDebug("Creating prepared statement using:\n%s\n", statement_text.c_str());
std::string filename("my_image.jpg");
Ptr_Db_Connection                           db_conn(db_mgr.get_db_connection());
boost::shared_ptr<sql::PreparedStatement>   prepared_statement(db_conn->prepareStatement(statement_text));
prepared_statement->setInt(1, picture_id);
std::ifstream   blob_file(filename.c_str());
prepared_statement->setBlob(2, &blob_file);
prepared_statement->execute();
blob_file.close();

这是表的架构:

mysql> describe picture_image_data;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| ID_Picture | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Image_Data | mediumblob       | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

从 MySql 控制台:

mysql> select ID_Picture, LENGTH(image_data)
    -> FROM picture_image_data
    -> where ID_Picture = 1;
+------------+--------------------+
| ID_Picture | LENGTH(image_data) |
+------------+--------------------+
|          1 |                 65 |
+------------+--------------------+
1 row in set (0.02 sec)

如何使准备好的语句读取整个文件?
我是否需要在 MySql 连接器 C++ 中初始化任何内容以使其读取超过 64 个字节?

注意:我在 Windows XP 和 Vista 上使用 MySql Connector C++ 1.0.5、Visual Studio 2008 和 wxWidgets。

建表语句:

CREATE TABLE Picture_Image_Data
(
    ID_Picture INTEGER UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
    Image_Data MEDIUMBLOB
);

图像文件的十六进制转储(通过 Cygwin):

0000000 d8ff e0ff 1000 464a 4649 0100 0101 1c00
0000010 1c00 0000 dbff 4300 0500 0403 0404 0503
0000020 0404 0504 0505 0706 080c 0707 0707 0b0f
0000030 090b 110c 120f 1112 110f 1311 1c16 1317
0000040 1a14 1115 1811 1821 1d1a 1f1d 1f1f 1713
0000050 2422 1e22 1c24 1f1e ff1e 00db 0143 0505
0000060 0705 0706 080e 0e08 141e 1411 1e1e 1e1e
0000070 1e1e 1e1e 1e1e 1e1e 1e1e 1e1e 1e1e 1e1e  
4

1 回答 1

1

问题在于图像文件的构造函数:

std::ifstream   blob_file(filename.c_str());

这应该具有二进制模式属性:

std::ifstream   blob_file(filename.c_str(), std::ios_base::binary);

该文件是 JPEG 图像,是二进制数据。

此外,字节 65 处的十六进制转储显示1a,这是 Windows 操作系统文件结尾字符:
0000040 1a 14 1115 1811 1821 1d1a 1f1d 1f1f 1713

修复构造函数后,MySql 显示数据大小:

mysql> SELECT ID_Picture, LENGTH(Image_Data)
    -> FROM picture_image_data
    -> WHERE ID_Picture = 1;
+------------+--------------------+
| ID_Picture | LENGTH(Image_Data) |
+------------+--------------------+
|          1 |              18453 |
+------------+--------------------+
1 row in set (0.00 sec)
于 2010-06-28T17:33:52.643 回答