C++获取mysql中Blob字段的数据(图片)二

C++获取mysql中Blob字段的数据(图片)二,第1张

方法一:https://zhangzc.blog.csdn.net/article/details/124150873

使用c API预处理语句函数,直接上代码:

要获取的表中的数据:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;


int main()
{
	//1.初始化环境
	MYSQL* mySql = mysql_init(NULL);
	if (mySql == NULL)
	{
		printf("数据库环境初始化失败!\n");
		return -1;
	}

	//2.连接数据库
	mySql = mysql_real_connect(mySql, "localhost", "root", "root", "zzc", 3306, NULL, 0);
	if (mySql == NULL)
	{
		printf("数据库连接失败!\n");
		return -1;
	}

	mysql_set_character_set(mySql, "gbk");

	//初始化结构MYSQL_STMT
	MYSQL_STMT    *stmt;
	stmt = mysql_stmt_init(mySql);
	if (!stmt)
	{
		printf(" mysql_stmt_init(), out of memory\n");
	}

	//准备
	char sql[200] = "select id, name, time, photo from student";
	if (mysql_stmt_prepare(stmt, sql, strlen(sql)))
	{
		printf(" mysql_stmt_prepare(), SELECT failed\n");
		printf(" %s\n", mysql_stmt_error(stmt));
	}

	//获取参数标记符(?)的个数,此处是0个
	int param_count = mysql_stmt_param_count(stmt);
	printf(" total parameters in SELECT: %d\n", param_count);

	MYSQL_RES *prepare_meta_result;

	prepare_meta_result = mysql_stmt_result_metadata(stmt);
	int column_count = mysql_num_fields(prepare_meta_result);
	fprintf(stdout, " total columns in SELECT statement: %d\n", column_count);

	//执行查询
	if (mysql_stmt_execute(stmt))
	{
		printf(" mysql_stmt_execute(), failed\n");
		printf(" %s\n", mysql_stmt_error(stmt));
	}

	//绑定
	MYSQL_BIND    bind[4];
	unsigned long length[4];
	bool       is_null[4];

	memset(bind, 0, sizeof(bind));
	memset(length, 0, sizeof(length));
	memset(is_null, 0, sizeof(is_null));

	int int_data = 0;
	bind[0].buffer_type = MYSQL_TYPE_LONG;
	bind[0].buffer = (char *)&int_data;
	bind[0].is_null = &is_null[0];
	bind[0].length = &length[0];

	char str_data[260] = {0};
	bind[1].buffer_type = MYSQL_TYPE_STRING;
	bind[1].buffer = str_data;
	bind[1].buffer_length = 260;
	bind[1].is_null = &is_null[1];
	bind[1].length = &length[1];

	MYSQL_TIME mt;
	bind[2].buffer_type = MYSQL_TYPE_DATETIME;
	bind[2].buffer = (char *)&mt;
	bind[2].is_null = &is_null[2];
	bind[2].length = &length[2];

	char* pData = new char [1024 * 1024];
	memset(pData, 0x00, 1024 * 1024);
	bind[3].buffer_type = MYSQL_TYPE_MEDIUM_BLOB;
	bind[3].buffer = pData;
	bind[3].buffer_length = 1024 * 1024;
	bind[3].is_null = &is_null[3];
	bind[3].length = &length[3];

	if (mysql_stmt_bind_result(stmt, bind))
	{
		printf(" mysql_stmt_bind_result() failed\n");
		printf(" %s\n", mysql_stmt_error(stmt));
	}

	//获取结果集
	if (mysql_stmt_store_result(stmt))
	{
		printf(" mysql_stmt_store_result() failed\n");
		printf(" %s\n", mysql_stmt_error(stmt));
	}

	int rows = mysql_stmt_num_rows(stmt);
	printf("totalRows: %d\n", rows);

	//循环遍历结果集
	int row_count = 0;
	int nRet = 0;
	while (!(nRet = mysql_stmt_fetch(stmt)))
	{
		row_count++;
		printf("  row %d\n", row_count);

		printf("   column1 (integer)  : ");
		if (is_null[0])
			printf(" NULL\n");
		else
			printf(" %d(%ld)\n", int_data, length[0]);

		printf("   column2 (string)   : ");
		if (is_null[1])
			printf(" NULL\n");
		else
			printf(" %s(%ld)\n", str_data, length[1]);

		printf("   column3 (datetime) : ");
		if (is_null[2])
			printf(" NULL\n");
		else
			printf(" %04d-%02d-%02d %02d:%02d:%02d(%ld)\n", mt.year,mt.month,mt.day,mt.hour,mt.minute,mt.second, length[2]);

		printf("   column4 (medium_date): ");
		if (is_null[3])
			printf(" NULL\n");
		else
		{
			stringstream pic_name;
			pic_name << "D:\" << row_count + 10 << ".jpg";
			ofstream outfile(pic_name.str(), ios::binary);
			outfile.write(pData, length[3]);
		}
			
		printf("\n");
	}

	printf("nRet: %d\n", nRet);

	if (pData) delete[] pData, pData = NULL;

	mysql_free_result(prepare_meta_result);

	//资源销毁
	if (mysql_stmt_close(stmt))
	{
		printf(" failed while closing the statement\n");
		printf(" %s\n", mysql_stmt_error(stmt));
	}

	//释放资源-数据库
	if (mySql)
	{
		mysql_close(mySql);
	}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/634717.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-16
下一篇 2022-04-16

发表评论

登录后才能评论

评论列表(0条)

保存