Linux C/C++ 实现MySQL的图片插入以及图片的读取

Posted 别,爱℡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux C/C++ 实现MySQL的图片插入以及图片的读取相关的知识,希望对你有一定的参考价值。


欢迎大家来到别爱的CSDN


前言

生活中大多数的场景都不单单只是数据,还有其它许多信息,今天就给大家介绍一种比较常见的数据:图片。如何实现图片的插入数据库,以及数据库图片的读取


提示:以下是本篇文章正文内容,下面案例可供参考

一、代码实现

1.服务端读取图片

代码如下(示例):

int read_image(char* filename,char* buffer) 

	if (filename == NULL || buffer == NULL)return -1;

	FILE* fp = fopen(filename, "rb");
	if (fp == NULL) 
		printf("fopen failed\\n");
		return -2;
	

	//file size
	fseek(fp, 0, SEEK_END);
	int length = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	int size = fread(buffer, 1, length, fp);
	if (size != length) 
		printf("fread failed: %d\\n",size);
		return size;
	

	fclose(fp);

	return size;


2.服务端写入图片

代码如下(示例):

int write_image(char* filename, char* buffer, int length) 

	if (filename == NULL || buffer == NULL || length <= 0) return -1;

	FILE* fp = fopen(filename, "wb+");
	if (fp == NULL) 
		printf("fopen failed\\n");
		return -2;
	
	
	int size = fwrite(buffer, 1, length, fp);
	if (size != length) 
		printf("fwrite failed: %d\\n", size);
		return -3;
	

	fclose(fp);
	return size;

3.数据库读取图片

代码如下(示例):

int mysql_read(MYSQL* handle, char* buffer, int length) 

	if (handle == NULL || buffer == NULL || length <= 0)return -1;

	MYSQL_STMT* stmt = mysql_stmt_init(handle);
	int ret = mysql_stmt_prepare(stmt, SQL_SELECT_IMG_USER, strlen(SQL_SELECT_IMG_USER));
	if (ret) 
		printf("mysql_stmt_prepare :%s\\n", mysql_error(handle));
		return -2;
	

	MYSQL_BIND result =  0 ;

	result.buffer_type = MYSQL_TYPE_LONG_BLOB;
	unsigned long total_length = 0;
	result.length = &total_length;

	ret = mysql_stmt_bind_result(stmt, &result);
	if (ret) 
		printf("mysql_stmt_bind_result: %s\\n", mysql_error(handle));
		return -3;
	

	ret = mysql_stmt_execute(stmt);
	if (ret) 
		printf("mysql_stmt_execute :%s\\n", mysql_error(handle));
		return -4;
	

	ret = mysql_stmt_store_result(stmt);
	if (ret) 
		printf("mysql_stmt_store_result: %s\\n", mysql_error(handle));
		return -5;
	


	while (1) 

		ret = mysql_stmt_fetch(stmt);
		if (ret != 0 && ret != MYSQL_DATA_TRUNCATED) break;	

		int start = 0;
		while (start < (int)total_length) 
			result.buffer = buffer + start;
			result.buffer_length = 1;
			mysql_stmt_fetch_column(stmt, &result, 0, start);
			start += result.buffer_length;
		

	

	mysql_stmt_close(stmt);

	return total_length;


4.数据库写入图片

代码如下(示例):

int mysql_write(MYSQL* handle, char* buffer, int length) 

	if (handle == NULL || handle == NULL || length <= 0)return -1;

	MYSQL_STMT* stmt = mysql_stmt_init(handle);
	int ret = mysql_stmt_prepare(stmt, SQL_INSERT_IMG_USER, strlen(SQL_INSERT_IMG_USER));
	if (ret) 
		printf("mysql_stmt_prepare: %s\\n", mysql_error(handle));
		return -2;
	

	MYSQL_BIND param =  0 ;
	param.buffer_type = MYSQL_TYPE_LONG_BLOB;
	param.buffer = NULL;
	param.is_null = 0;
	param.length = NULL;

	 ret = mysql_stmt_bind_param(stmt, &param);
	if (ret) 
		printf("mysql_stmt_bind_param :%s\\n", mysql_error(handle));
		return -3;
	

	ret = mysql_stmt_send_long_data(stmt, 0, buffer, length);
	if (ret) 
		printf("mysql_stmt_send_long_data :%s\\n", mysql_error(handle));
		return -4;
	

	ret = mysql_stmt_execute(stmt);
	if (ret) 
		printf("mysql_stmt_execute: %s\\n", mysql_error(handle));
		return -5;
	

	ret = mysql_stmt_close(stmt);
	if (ret) 
		printf("mysql_stmt_close :%s\\n", mysql_error(handle));
		return -6;
	

	return ret;

5.全部代码(以及部分注释)

代码如下(示例):

#include<stdio.h>
#include<string.h>
#include<mysql.h>

/// 把一些常量定义在这,使代码变得可读性更高
#define BIEAI_DB_SERVER_IP "192.168.230.128"	//ip地址
#define BIEAI_DB_SERVER_PORT 3306	//端口号
#define BIEAI_DB_USERNAME "admin"	//用户名
#define BIEAI_DB_PASSWORD "131420"	//密码
#define BIEAI_DB_DEFAULTDB "BIEAI_DB"	//数据库名
#define SQL_INSERT_IMG_USER "INSERT TBL_USER(U_NAME,U_GENGDER,U_IMG) VALUES('wangwu','boy',?);"//数据库插入语句
#define SQL_SELECT_IMG_USER "SELECT U_IMG FROM TBL_USER WHERE U_NAME='wangwu';"	//数据库查询语句
#define FILE_IMAGE_LENGTH	(64*1024)//64k大小



int read_image(char* filename,char* buffer) 

	if (filename == NULL || buffer == NULL)return -1;

	FILE* fp = fopen(filename, "rb");//读权限
	if (fp == NULL) 
		printf("fopen failed\\n");
		return -2;
	

	//file size
	fseek(fp, 0, SEEK_END);
	int length = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	int size = fread(buffer, 1, length, fp);
	if (size != length) 
		printf("fread failed: %d\\n",size);
		return size;
	

	fclose(fp);

	return size;




int write_image(char* filename, char* buffer, int length) 

	if (filename == NULL || buffer == NULL || length <= 0) return -1;

	FILE* fp = fopen(filename, "wb+");//继续写
	if (fp == NULL) 
		printf("fopen failed\\n");
		return -2;
	
	
	int size = fwrite(buffer, 1, length, fp);
	if (size != length) 
		printf("fwrite failed: %d\\n", size);
		return -3;
	

	fclose(fp);
	return size;



int mysql_write(MYSQL* handle, char* buffer, int length) 

	if (handle == NULL || handle == NULL || length <= 0)return -1;

	MYSQL_STMT* stmt = mysql_stmt_init(handle);
	int ret = mysql_stmt_prepare(stmt, SQL_INSERT_IMG_USER, strlen(SQL_INSERT_IMG_USER));
	if (ret) 
		printf("mysql_stmt_prepare: %s\\n", mysql_error(handle));
		return -2;
	

	MYSQL_BIND param =  0 ;
	param.buffer_type = MYSQL_TYPE_LONG_BLOB;
	param.buffer = NULL;
	param.is_null = 0;
	param.length = NULL;

	 ret = mysql_stmt_bind_param(stmt, &param);
	if (ret) 
		printf("mysql_stmt_bind_param :%s\\n", mysql_error(handle));
		return -3;
	

	ret = mysql_stmt_send_long_data(stmt, 0, buffer, length);
	if (ret) 
		printf("mysql_stmt_send_long_data :%s\\n", mysql_error(handle));
		return -4;
	

	ret = mysql_stmt_execute(stmt);
	if (ret) 
		printf("mysql_stmt_execute: %s\\n", mysql_error(handle));
		return -5;
	

	ret = mysql_stmt_close(stmt);
	if (ret) 
		printf("mysql_stmt_close :%s\\n", mysql_error(handle));
		return -6;
	

	return ret;



int mysql_read(MYSQL* handle, char* buffer, int length) 

	if (handle == NULL || buffer == NULL || length <= 0)return -1;

	MYSQL_STMT* stmt = mysql_stmt_init(handle);
	int ret = mysql_stmt_prepare(stmt, SQL_SELECT_IMG_USER, strlen(SQL_SELECT_IMG_USER));
	if (ret) 
		printf("mysql_stmt_prepare :%s\\n", mysql_error(handle));
		return -2;
	

	MYSQL_BIND result =  0 ;

	result.buffer_type = MYSQL_TYPE_LONG_BLOB;
	unsigned long total_length = 0;
	result.length = &total_length;

	ret = mysql_stmt_bind_result(stmt, &result);
	if (ret) 
		printf("mysql_stmt_bind_result: %s\\n", mysql_error(handle));
		return -3;
	

	ret = mysql_stmt_execute(stmt);
	if (ret) 
		printf("mysql_stmt_execute :%s\\n", mysql_error(handle));
		return -4;
	

	ret = mysql_stmt_store_result(stmt);
	if (ret) 
		printf("mysql_stmt_store_result: %s\\n", mysql_error(handle));
		return -5;
	


	while (1) 

		ret = mysql_stmt_fetch(stmt);
		if (ret != 0 && ret != MYSQL_DATA_TRUNCATED) break;	

		int start = 0;
		while (start < (int)total_length) 
			result.buffer = buffer + start;
			result.buffer_length = 1;
			mysql_stmt_fetch_column(stmt, &result, 0, start);
			start += result.buffer_length;
		

	

	mysql_stmt_close(stmt);

	return total_length;








int main() 

	MYSQL mysql;

	if (NULL == mysql_init(&mysql)) 

		printf("mysql_init :%s\\n", mysql_error(&mysql));
		return -1;
	

	if (!mysql_real_connect(&mysql, BIEAI_DB_SERVER_IP, BIEAI_DB_USERNAME, BIEAI_DB_PASSWORD, BIEAI_DB_DEFAULTDB, BIEAI_DB_SERVER_PORT, NULL, 0)) //返回0成功

		printf("mysql_real_connect :%s\\n", mysql_error(&mysql));
		return -2;
	


	printf("case: mysql --> read image and write mysql\\n");

	char buffer[FILE_IMAGE_LENGTH] =  0 ;
	int length = read_image("/home/root01/linux/05MYSQL/Linux.JPG", buffer);
	if (length < 0) goto Exit;

	mysql_write(&mysql, buffer, length);		//插入数据库

	printf("\\n\\n");

	printf("case: mysql --> read mysql and write image\\n");

	memset(buffer, 0, FILE_IMAGE_LENGTH);
	length = mysql_read(&mysql, buffer, FILE_IMAGE_LENGTH);

	write_image("a.jpg", buffer, length);		//写出数据库




Exit:
	mysql_close(&mysql);

	return 0;






总结

今天主要给大家带来了MySQL数据库的图片插入,以及图片的读取,上述遇到不清楚的函数,下篇博客我会统一介绍。

参考资料

C/C++ Linux高级开发课程

以上是关于Linux C/C++ 实现MySQL的图片插入以及图片的读取的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习之CentOS--CentOS6.5下Mysql数据库的安装及C/C++连接使用Mysql

Linux学习之CentOS--CentOS6.5下Mysql数据库的安装及C/C++连接使用Mysql

Linux学习之CentOS--CentOS6.5下Mysql数据库的安装及C/C++连接使用Mysql

C/C++ 操作mysql字符集设置

MySQL 语法混淆 - 合并简单数据以实现优雅的单行插入

C/C++实现生产者消费者模式