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

相关推荐
不去幼儿园22 分钟前
【MARL】深入理解多智能体近端策略优化(MAPPO)算法与调参
人工智能·python·算法·机器学习·强化学习
Mr_Xuhhh24 分钟前
重生之我在学环境变量
linux·运维·服务器·前端·chrome·算法
Ajiang28247353041 小时前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++
盼海1 小时前
排序算法(五)--归并排序
数据结构·算法·排序算法
幽兰的天空1 小时前
Python 中的模式匹配:深入了解 match 语句
开发语言·python
远歌已逝2 小时前
维护在线重做日志(二)
数据库·oracle
qq_433099403 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz3 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
Theodore_10224 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou5 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot