Mysql LONGBLOB 类型存储二进制数据 (修改+调试+整理
网络编程 2021-07-05 15:27www.168986.cn编程入门
代码来自网络,我学习整理了一下,测试通过,狼蚁网站SEO优化的参数需要设置为你自己的
在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 <Winsock2.h>
#include <mysql.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ftl.h>
#pragma ment(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)) < 0) //读文件失败
{
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;
}
运行结果:
//测试文件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 <Winsock2.h>
#include <mysql.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ftl.h>
#pragma ment(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)) < 0) //读文件失败
{
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;
}
运行结果:
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程