您好,欢迎访问一九零五行业门户网

Mysql LONGBLOB 类型存储二进制数据 (修改+调试+整理)

代码来自网络,我学习整理了一下,测试通过,下面的参数需要设置为你自己的
在dbms中线要创建数据库test,table bintest,data字段数据类型用longblob即可测试
//测试文件c:\\test.iso,你可以找任何一个文件修改为即可,我找的是一个exe程序,修改为test.iso而已
//最大测试过加入文件大小为650m(一个正真的iso文件)
//注意:还要修改my.ini文件中的max_allowed_packet字段,我设置的是
代码如下:
//max_allowed_packet = 1024m
//#define host localhost //mysql server
//#define username root
//#define password 674800
//#define database test
//int port = 3306;
// mysql3.cpp : defines the entry point for the console application.
//
#include stdafx.h
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment(lib,libmysql.lib)
#define insert_query insert into bintest(id, data) values(null, ?)
#define host localhost //mysql server
#define username root
#define password 674800
#define database test
int port = 3306;
int get_file_size(char *path, off_t *size)
{
struct stat file_stats;
if(stat(path, &file_stats))
return -1;
*size = file_stats.st_size;
return 0;
}
void test()
{
mysql_bind bind[1];
unsigned long length;
char* pos = null;
off_t size;
file* fp;
char* filename = c:\\test.iso;
if ((get_file_size(filename, &size)) == -1) //得到文件的大小
{
perror(get file size );
exit(1);
}
if ((pos = (char *)malloc(sizeof(char)*(size+1))) == null)
{
perror(malloc buf );
exit(1);
}
if ((fp = fopen(filename, rb )) == null) //读文件
{
perror(fopen file );
exit(1);
}
if ((fread(pos, 1, size, fp)) {
perror(fread file );
exit(1);
}
mysql *mysql = mysql_init(null); //mysql 初始化
if (!mysql)
return;
if (!mysql_real_connect(mysql,host,username,password,test,port,null,0))//链接服务器
{
int ret = mysql_errno(mysql);
mysql_close(mysql);
return;
}
mysql_stmt *stmt = mysql_stmt_init(mysql);
if (!stmt)
{
fprintf(stderr, mysql_stmt_init(), out of memory\n);
exit(0);
}
if (mysql_stmt_prepare(stmt, insert_query, strlen(insert_query)))
{
fprintf(stderr, \n mysql_stmt_prepare(), insert failed);
fprintf(stderr, \n %s, mysql_stmt_error(stmt));
exit(0);
}
memset(bind, 0, sizeof(bind));
//bind[0].buffer_type= mysql_type_string;
//bind[0].buffer_type = mysql_type_long;
bind[0].buffer = pos;
//bind[0].buffer_type = mysql_type_tiny;
bind[0].buffer_type = mysql_type_blob;
bind[0].length= &length;
bind[0].is_null= 0;
/* bind the buffers */
if (mysql_stmt_bind_param(stmt, bind))
{
fprintf(stderr, \n param bind failed);
fprintf(stderr, \n %s, mysql_stmt_error(stmt));
exit(0);
}
int rc =0;
/* supply data in chunks to server */
if (mysql_stmt_send_long_data(stmt,0, pos, size))
{
fprintf(stderr, \n send_long_data failed);
fprintf(stderr, \n %s, mysql_stmt_error(stmt));
exit(0);
}
// pos += size;
/* supply the next piece of data */
if (mysql_stmt_send_long_data(stmt,0, pos, size))
{
fprintf(stderr, \n send_long_data failed);
fprintf(stderr, \n %s, mysql_stmt_error(stmt));
exit(0);
}
/* now, execute the query */
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, \n mysql_stmt_execute failed);
fprintf(stderr, \n %s, mysql_stmt_error(stmt));
exit(0);
}
}
int main()
{
test();
//sleep(1);
return 0;
}
运行结果:
其它类似信息

推荐信息