c/vc/c++ 将mysql二进制字段(longblob类型)读出来保存成文件
2012-03-16 13:41
633 查看
最后的写文件操作,用的是cfile,记得选择要支持mfc
#include "StdAfx.h"
#include <afxwin.h>
#include <stdio.h>
#include "winsock2.h"
#pragma comment(lib,"ws2_32")
#include <mysql.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "mysql.h"
#include "stdio.h"
#include "io.h"
#include "sys/stat.h"
#include <FCNTL.H>
#include<stdlib.h>
#pragma comment(lib,"libmySQL.lib")
#pragma comment(lib,"ws2_32")
void test_write(MYSQL& client)
{
MYSQL_STMT* stmt = mysql_stmt_init(&client);
assert(NULL!=stmt);
const char* sql = "select file from tablename2";
int sql_len = strlen(sql);
int ret = mysql_stmt_prepare(stmt, sql, sql_len);
assert(0==ret);
// ERR_LOG("param count:%d", (int)mysql_stmt_param_count(stmt));
//
MYSQL_BIND result = {0};
unsigned long total_length = 0;
result.buffer_type = MYSQL_TYPE_LONG_BLOB;
result.length = &total_length;
ret = mysql_stmt_bind_result(stmt, &result);
assert(0==ret);
ret = mysql_stmt_execute(stmt);
assert(0==ret);
ret = mysql_stmt_store_result(stmt);
assert(0==ret);
//while (mysql_stmt_fetch(stmt)!=0)
for (;;)
{
ret = mysql_stmt_fetch(stmt);
if (ret!=0 && ret!=MYSQL_DATA_TRUNCATED) break;
int start = 0;
char *buf=new char[total_length];
memset(buf,0,sizeof(buf));
printf("total_length=%lu\n", total_length);
while (start<(int)total_length)
{
result.buffer = (buf+start);
result.buffer_length = 3; //每次读这么长
ret = mysql_stmt_fetch_column(stmt, &result, 0, start);
if (ret!=0)
{
return;
}
start += result.buffer_length;
}
CFile f;
if(f.Open("e:\\aaa.7z", CFile::modeCreate|CFile::modeWrite | CFile::typeBinary))
{
f.WriteHuge(buf,total_length);
f.Close();
}
}
mysql_stmt_close(stmt);
}
int main(int argc, char* argv[])
{
MYSQL client;
mysql_init(&client);
if ((mysql_real_connect(&client, "192.168.1.101", "root", "", "test", 0, NULL, 0)) == NULL) {
printf("connect mysql, %sn", mysql_error(&client));
return 1;
}
test_write(client);
//ERR_LOG("OK");
return ;
}
#include "StdAfx.h"
#include <afxwin.h>
#include <stdio.h>
#include "winsock2.h"
#pragma comment(lib,"ws2_32")
#include <mysql.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "mysql.h"
#include "stdio.h"
#include "io.h"
#include "sys/stat.h"
#include <FCNTL.H>
#include<stdlib.h>
#pragma comment(lib,"libmySQL.lib")
#pragma comment(lib,"ws2_32")
void test_write(MYSQL& client)
{
MYSQL_STMT* stmt = mysql_stmt_init(&client);
assert(NULL!=stmt);
const char* sql = "select file from tablename2";
int sql_len = strlen(sql);
int ret = mysql_stmt_prepare(stmt, sql, sql_len);
assert(0==ret);
// ERR_LOG("param count:%d", (int)mysql_stmt_param_count(stmt));
//
MYSQL_BIND result = {0};
unsigned long total_length = 0;
result.buffer_type = MYSQL_TYPE_LONG_BLOB;
result.length = &total_length;
ret = mysql_stmt_bind_result(stmt, &result);
assert(0==ret);
ret = mysql_stmt_execute(stmt);
assert(0==ret);
ret = mysql_stmt_store_result(stmt);
assert(0==ret);
//while (mysql_stmt_fetch(stmt)!=0)
for (;;)
{
ret = mysql_stmt_fetch(stmt);
if (ret!=0 && ret!=MYSQL_DATA_TRUNCATED) break;
int start = 0;
char *buf=new char[total_length];
memset(buf,0,sizeof(buf));
printf("total_length=%lu\n", total_length);
while (start<(int)total_length)
{
result.buffer = (buf+start);
result.buffer_length = 3; //每次读这么长
ret = mysql_stmt_fetch_column(stmt, &result, 0, start);
if (ret!=0)
{
return;
}
start += result.buffer_length;
}
CFile f;
if(f.Open("e:\\aaa.7z", CFile::modeCreate|CFile::modeWrite | CFile::typeBinary))
{
f.WriteHuge(buf,total_length);
f.Close();
}
}
mysql_stmt_close(stmt);
}
int main(int argc, char* argv[])
{
MYSQL client;
mysql_init(&client);
if ((mysql_real_connect(&client, "192.168.1.101", "root", "", "test", 0, NULL, 0)) == NULL) {
printf("connect mysql, %sn", mysql_error(&client));
return 1;
}
test_write(client);
//ERR_LOG("OK");
return ;
}
相关文章推荐
- c/vc/c++ 将文件保存到mysql数据库(longblob类型)
- MySql中LongBlob类型字段分次读取生成本地文件
- VC、C++保存二叉树在文件中然后读出来
- 如何用JAVA将二进制文件转换成BASE64格式保存到MySQL的Blob字段里并读出下载
- C++读取二进制文件到二维数组并保存到文本文件
- c++ 二进制保存文件
- mysql日期字段类型---如何保存create_time和update_time
- MySql 读取和写入LONGBLOB 类型字段。
- 在Mysql中用 LONGBLOB 类型存储二进制数据
- 自制工具:CSV代码生成器:自动生成CSV文件对应的C++实体类和字段类型解析代码
- VFP将图片或其他文件保存到SQL Server的Text类型字段或者Access的备注字段中,并可以还原或显示。
- 在Mysql中用 LONGBLOB 类型存储二进制数据
- 学生成绩处理:保存为二进制文件 ASCII 文件score.dat 中保存的是100 名学生的姓名和C++课、高 数和英语成绩。
- 保存在数据中image字段的图片文件显示出来
- java将文件保存为二进制流到oracle表中的blob字段
- mysql 字段类型保存小数和整数
- 保存在数据中image字段的图片文件显示出来
- Mybatis解决空字符串保存入Mysql数字字段的类型转换问题
- 关于C++二进制文件读取转换,数据版本升级,增加字段等结构调整工具的思路。
- 使用数据库(mysql)字段保存文件