什么是准系统 C++将 jpeg 放入 MySQL 表所需的代码吗?
我创建了一个 MySQL 表,其中一列存储 BLOB 类型。 (互联网告诉我 BLOB 是图像的正确数据类型。)
我几乎是 C++ 和 MySQL 的初学者。我想做的是编写一个带有 main() 的小程序,将 jpeg 放入该表中。为了进行本练习,我不想存储对包含图像的目录的引用。
我是否错误地认为这就像填写下面第 2 块中的部分一样简单?
#include <iostream>
#include <string>
#include <mysql.h>
using namespace std;
int main(int argc, char **argv)
{
//BLOCK 1: INIT
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, "localhost", "root", "secret", "beginner_db",0,0,0);
//BLOCK 2: SEND QUERY
/* do something to insert image to table */
//BLOCK 3: DISPLAY QUERY RESULTS
result = mysql_store_result(connection);
/* do something with result */
//BLOCK 4: FREE
mysql_free_result(result);
mysql_close(connection);
return 0;
}
I have created a MySQL table where one of the columns stores a BLOB type. (The Internet told me BLOB is the correct data type for images.)
I am pretty much a beginner with both C++ and MySQL. What I would like to do is to write a small program with a main() that puts a jpeg into that table. For the sake of this exercise, I do not want to store a reference to a directory that contains an image.
Am I wrong to think that it is as simple as filling out the part in BLOCK 2 below?
#include <iostream>
#include <string>
#include <mysql.h>
using namespace std;
int main(int argc, char **argv)
{
//BLOCK 1: INIT
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, "localhost", "root", "secret", "beginner_db",0,0,0);
//BLOCK 2: SEND QUERY
/* do something to insert image to table */
//BLOCK 3: DISPLAY QUERY RESULTS
result = mysql_store_result(connection);
/* do something with result */
//BLOCK 4: FREE
mysql_free_result(result);
mysql_close(connection);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于这种情况,一个好的解决方案是使用
mysql_stmt_send_long_data()
函数。我链接到的 MySQL 手册页面上有一个示例,但这里有一个更相关的发送文件内容的示例:
我正在使用表
`images`
的以下定义:运行此程序时,它成功地将 38,339 字节的 JPEG
image.jpg
发送到服务器并输出“Inserted record #1”。您可以验证发送的字节数是否正确:
For this scenario, a good solution would be to use the
mysql_stmt_send_long_data()
function.There is an example on the MySQL Manual page that I linked to, but here is a more relevant example of sending file contents:
I am using the following definition of table
`images`
:Upon running this program, it successfully sent the 38,339-byte JPEG
image.jpg
to the server and outputted "Inserted record #1".You can verify that the correct number of bytes were sent:
我发现这个解决方案适用于 10kb 以下的图像。
I found this solution that worked... for images under 10kb.
像这样的东西:
此代码未经测试,甚至未编译,但它应该为您指明正确的方向。
Something like this:
This code is not tested or even compiled, but it should point you in the right direction.