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

相关推荐
唐诺2 分钟前
几种广泛使用的 C++ 编译器
c++·编译器
工业甲酰苯胺6 分钟前
分布式系统架构:服务容错
数据库·架构
XH华11 分钟前
初识C语言之二维数组(下)
c语言·算法
南宫生32 分钟前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
不想当程序猿_44 分钟前
【蓝桥杯每日一题】求和——前缀和
算法·前缀和·蓝桥杯
高山我梦口香糖1 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
落魄君子1 小时前
GA-BP分类-遗传算法(Genetic Algorithm)和反向传播算法(Backpropagation)
算法·分类·数据挖掘
冷眼看人间恩怨1 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
菜鸡中的奋斗鸡→挣扎鸡1 小时前
滑动窗口 + 算法复习
数据结构·算法
独行soc1 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍08-基于时间延迟的SQL注入(Time-Based SQL Injection)
数据库·sql·安全·渗透测试·漏洞挖掘