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

相关推荐
0白露38 分钟前
Apifox Helper 与 Swagger3 区别
开发语言
Tanecious.2 小时前
机器视觉--python基础语法
开发语言·python
叠叠乐2 小时前
rust Send Sync 以及对象安全和对象不安全
开发语言·安全·rust
想跑步的小弱鸡2 小时前
Leetcode hot 100(day 3)
算法·leetcode·职场和发展
·薯条大王2 小时前
MySQL联合查询
数据库·mysql
Tttian6223 小时前
Python办公自动化(3)对Excel的操作
开发语言·python·excel
xyliiiiiL3 小时前
ZGC初步了解
java·jvm·算法
爱的叹息4 小时前
RedisTemplate 的 6 个可配置序列化器属性对比
算法·哈希算法
morris1314 小时前
【redis】redis实现分布式锁
数据库·redis·缓存·分布式锁
Merokes4 小时前
关于Gstreamer+MPP硬件加速推流问题:视频输入video0被占用
c++·音视频·rk3588