数据库中二进制数据图片的读取并存储为文件C++

2024-01-03 06:01:09

插入二进制数据https://blog.csdn.net/wjl990316fddwjl/article/details/135334950我们需要将插入的数据进行读取,本文主要介绍如何C++如何将数据库中二进制数据图片进行读取和存储

1、查询数据

sql="select * from t_data";

2、获得查询结果

MYSQL_RES *res=mysql_store_result(&mysql);

?3、获取行数据

MYSQL_ROW row=mysql_fetch_row(res);

4、?写入文件

fstream out(filename_out,ios::out|ios::binary);
out.write(row[2],lens[2]);
//查询二进制数据,并保存为文件
	//sql = "select * from t_data limit 1"; //只查找第一个,效率会更高v
	sql = "select * from t_data";
	re = mysql_query(&mysql, sql.c_str());
	if (re != 0)
	{
		cerr << "mysql_query failed" << mysql_error(&mysql) << endl;
	}
	MYSQL_RES* res = mysql_store_result(&mysql);
	if (!res)
	{
		cerr << "mysql_store_result failed" << mysql_error(&mysql) << endl;
	}
	//取一行数据 是一个数组
	MYSQL_ROW row = mysql_fetch_row(res);
	if (!row)
	{
		cerr << "mysql_fetch_row failed" << mysql_error(&mysql) << endl;
	}
    //如果没有size,可以获取size
	//获取每列数据的大小,这个是根据当前的
	unsigned long *lens = mysql_fetch_lengths(res);
	//具体写入文件
	string filename_out = "out_1";
	filename_out += row[1];
	//写入
	fstream out(filename_out,ios::out|ios::binary);

	if (!out.is_open())
	{
		cerr << "open_filename" << filename_out << "falied" << endl;
	}
	out.write(row[2], lens[2]);
	out.close();

查看是否存储成功

注意*****

必须将文件最后进行关闭

out.close();

完整代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<mysql.h>
#include<thread>
#include<map>
#include<sstream>
#include<fstream>
//计时
#include<chrono>
using namespace std;
using namespace chrono;


int main()
{
	const char* host = "127.0.0.1";
	const char* user = "root";
	const char* password = "******";
	const char* database = "database_test";
	int port = 3306;

	MYSQL mysql;
	mysql_init(&mysql);
	mysql_library_init(0, NULL, NULL);
	//防止乱码
	mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "GBK");

	//支持多条sql语句 CLIENT_MULTI_STATEMENTS   clientflag  默认是不支持的
	//if (!mysql_real_connect(&mysql, host, user, password, database, port, 0, 0))
	if (!mysql_real_connect(&mysql, host, user, password, database, port, NULL, 0))
	{
		cout << "failed to connect" << host << mysql_error(&mysql) << endl;
	}

	else
	{
		cout << "successfully connected " << host <<endl;
	}
	
	


	//查询二进制数据,并保存为文件
	//sql = "select * from t_data limit 1"; //只查找第一个,效率会更高v
	sql = "select * from t_data";
	re = mysql_query(&mysql, sql.c_str());
	if (re != 0)
	{
		cerr << "mysql_query failed" << mysql_error(&mysql) << endl;
	}
	MYSQL_RES* res = mysql_store_result(&mysql);
	if (!res)
	{
		cerr << "mysql_store_result failed" << mysql_error(&mysql) << endl;
	}
	//取一行数据 是一个数组
	MYSQL_ROW row = mysql_fetch_row(res);
	if (!row)
	{
		cerr << "mysql_fetch_row failed" << mysql_error(&mysql) << endl;
	}

	//存取文件 第三个字段取出来
	cout << row[0] << " " << row[1] << " " << row[3] << endl;

	//如果没有size,可以获取size
	//获取每列数据的大小,这个是根据当前的
	unsigned long *lens = mysql_fetch_lengths(res);
	//有几列,这个是根据结果来计算的
	int fnum = mysql_num_fields(res);
	for (int i = 0;i < fnum;i++)
	{
		cout << "[" << lens[i] << "]";
	}
	//具体写入文件
	string filename_out = "out_1";
	filename_out += row[1];
	//写入
	fstream out(filename_out,ios::out|ios::binary);

	if (!out.is_open())
	{
		cerr << "open_filename" << filename_out << "falied" << endl;
	}
	out.write(row[2], lens[2]);
	out.close();

	mysql_close(&mysql);
	mysql_library_end();
	system("pause");
	return 0;
}

文章来源:https://blog.csdn.net/wjl990316fddwjl/article/details/135342786
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。