C++使用map插入insert数据(二进制数据和非二进制数据)接口封装+读取文件

1、定义编写代码

cpp 复制代码
    //生成insert sql语句
	std::string GetInsertsql(XDATA kv, std::string table);
	//插入非二进制数据
	bool Insert(XDATA kv, std::string table);
	//插入二进制数据
	bool InsertBin(XDATA kv, std::string table);
cpp 复制代码
std::string LXMysql::GetInsertsql(XDATA kv, std::string table)
	{
		string sql = "";
		if (kv.empty() || table.empty())
		{
			return "";
		}
		else
		{
			sql = "insert into `";
			sql += table;
			sql += "` ";
			//insert into t_vedio (name,size) values ("name1","1024");
			string keys = "";
			string values = "";
			//用迭代map
			for (auto ptr = kv.begin();ptr != kv.end();ptr++)
			{
				keys += "`";
				keys += ptr->first;
				keys += "`,";

				values += "'";
				values += ptr->second.data;
				values += "',";
			}
			//去除结尾多余逗号
			keys[keys.size() - 1] = ' ';
			values[values.size() - 1] = ' ';
			sql += "(";
			sql += keys;
			sql += ")values(";
			sql += values;
			sql += ")";
			return sql;
		}		
	}
	bool LXMysql::Insert(XDATA kv, std::string table)
	{
		if (!mysql)//如果mysql没有初始化好
		{
			cerr << "Insert failed: mysql is NULL" << endl;
			return false;
		}
		string sql= GetInsertsql(kv, table);
		if (sql.empty())
		{
			return false;
		}
		if (!Query(sql.c_str()))
		{
			return false;
		}
		int num = mysql_affected_rows(mysql);
		if (num <= 0)
		{
			return false;
		}
		else
		{			
			return true;
		}		
	}
	bool LXMysql::InsertBin(XDATA kv, std::string table)
	{
		if (!mysql)
		{
			return false;
		}
		string sql = "";
		if (kv.empty() || table.empty())
		{
			return false;
		}
		else
		{
			sql = "insert into `";
			sql += table;
			sql += "` ";
			//insert into t_vedio (name,size) values (?,?);
			string keys = "";
			string values = "";
			//绑定
			MYSQL_BIND bind[256] = { 0 };
			int i = 0;

			//用迭代map
			for (auto ptr = kv.begin();ptr != kv.end();ptr++)
			{
				if (ptr->second.data==nullptr)
				{
					cout << "chdswvf" << endl;
				}
				keys += "`";
				keys += ptr->first;
				keys += "`,";

				values += "?,";
				bind[i].buffer = (char*)ptr->second.data;
				bind[i].buffer_length = ptr->second.size;
				//强转
				bind[i].buffer_type = (enum_field_types)ptr->second.type;
				i++;
			}
			//去除结尾多余逗号
			keys[keys.size() - 1] = ' ';
			values[values.size() - 1] = ' ';
			sql += "(";
			sql += keys;
			sql += ") values(";
			sql += values;
			sql += ")";
			//预处理sql语句
			MYSQL_STMT* stmt = mysql_stmt_init(mysql);
			if (!stmt)
			{
				cerr << "mysql_stmt_init failed " << mysql_error(mysql) << endl;
				return false;
			}
			if (mysql_stmt_prepare(stmt, sql.c_str(), sql.length()))
			{
				mysql_stmt_close(stmt);
				cerr << "mysql_stmt_prepare failed " << mysql_stmt_error(stmt) << endl;
				return false;
			}
			//绑定
			if (mysql_stmt_bind_param(stmt, bind) != 0)
			{
				mysql_stmt_close(stmt);
				cerr << "mysql_stmt_bind_param failed " << mysql_stmt_error(stmt) << endl;
				return false;
			}
			//执行
			if (mysql_stmt_execute(stmt) != 0)
			{
				mysql_stmt_close(stmt);
				cerr << "mysql_stmt_execute failed " << mysql_stmt_error(stmt) << endl;
				return false;
			}
			mysql_stmt_close(stmt);
			return true;		
			
		}
	}
cpp 复制代码
bool LXData::LoadFile(const char* filename)
	{
		//先读到本地
		if (filename == NULL)
		{
			return false;
		}
		else
		{
			fstream in(filename, ios::in | ios::binary);
			if (!in.is_open())
			{
				cerr << "LoadFile " << filename << "failed! " << endl;
				return false;
			}
			else
			{
				//文件大小
				in.seekg(0, ios::end);
				size = in.tellg();
				in.seekg(0, ios::beg);

				if (size <= 0)
				{
					return false;
				}
				else
				{
					data = new char[size];//记得释放
					//读数据
					int readed = 0;
					while (!in.eof())
					{
						in.read((char*)data + readed, size - readed);
						//表及读到那里了
						if (in.gcount() > 0)
						{
							readed = in.gcount();
						}
						else
						{
							break;
						}
						
					}
					in.close();
					this->type = LX_TYPE_BLOB;
					return true;
				}				
			}
			
		}
		
	}
	bool LXData::SaveFile(const char* filename)
	{
		if (!data || size <= 0)
		{
			return false;
		}
		fstream out(filename, ios::out | ios::binary);
		if (!out.is_open())
		{
			cerr << "SaveFile failed! open  " << filename << "failed!" << endl;
			return false;
		}
		out.write(data, size);
		out.close();
		return true;
	}
	void LXData::Drop()
	{
		delete data;
		data = NULL;
	}

3、测试

cpp 复制代码
//插入数据
    XDATA data1;  
    //data1.insert(make_pair("name", LXData("insertname001")));
    data1["name"]="insertname001";
    data1["size"] = "1024";
    //cout << my.GetInsertsql(data1, "t_vedio") << endl;
    my.Insert(data1, "t_vedio");
    data1["name"] = "insertname002";
    data1["size"] = "1024";
    my.Insert(data1, "t_vedio");
    //读取图片

    data1["name"] = "mysql_new.jpg";
    LXData file1;
    file1.LoadFile("F:/Documents/Desktop/c++/数据库plus/src/bin/mysql.jpg");
    data1["data"] = file1;
    data1["size"] = &file1.size;
    //正式插入数据
    my.InsertBin(data1, "t_vedio");

这篇代码比较复杂,我将可执行文件放在网盘,大家自行下载,最终输出如下,才算成功

链接:http://链接:https://pan.baidu.com/s/1ASdW9Hdv5q2X-ZK90YdDeQ?pwd=lldd 提取码:lldd

相关推荐
浮生如梦_35 分钟前
Halcon基于laws纹理特征的SVM分类
图像处理·人工智能·算法·支持向量机·计算机视觉·分类·视觉检测
CoderIsArt36 分钟前
Redis的三种模式:主从模式,哨兵与集群模式
数据库·redis·缓存
XiaoLeisj2 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
励志成为嵌入式工程师2 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
师太,答应老衲吧3 小时前
SQL实战训练之,力扣:2020. 无流量的帐户数(递归)
数据库·sql·leetcode
捕鲸叉3 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer3 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq3 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
Yaml43 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
wheeldown4 小时前
【数据结构】选择排序
数据结构·算法·排序算法