MySql Connector准备语句只传输64个字节 [英] MySql Connector prepared statement only transfers 64 bytes

查看:144
本文介绍了MySql Connector准备语句只传输64个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MySql Connector C ++将JPEG图片从文件存储到数据库中。我使用准备的语句。执行准备语句后,只有文件的前64个字节被复制到数据库中。

I am using the MySql Connector C++ to store a JPEG image from a file into the database. I am using the prepared statement. After execution of the prepared statement, only the first 64 bytes of the file are copied into the database.

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

My research of examples show that no iteration is necessary and the examples assume that the prepared statement loads the entire file. Here is my code:

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)

如何使预准备语句读取整个文件?

Do我需要初始化MySql连接器C ++中的任何东西,使这个读取超过64个字节?

How do I make the prepared statement read the entire file?
Do I need to initialize anything in MySql Connector C++ to make this read more than 64 bytes?

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

Note: I am using MySql Connector C++ 1.0.5, Visual Studio 2008 and wxWidgets on Windows XP and Vista.

表创建语句:

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

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

Hex dump (via Cygwin) of the image file:

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  


推荐答案

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

The issue lies in the constructor of the image file:

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

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

This should have the binary mode attribute:

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

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

The file, a JPEG image, is binary data.

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

Also, the hex dump at byte 65 shows 1a, which is the Windows OS end of file character:
0000040 *1a*14 1115 1811 1821 1d1a 1f1d 1f1f 1713

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

After fixing the constructor, the MySql shows the data size:

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)

这篇关于MySql Connector准备语句只传输64个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆