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

1、定义编写代码

    //生成insert sql语句
	std::string GetInsertsql(XDATA kv, std::string table);
	//插入非二进制数据
	bool Insert(XDATA kv, std::string table);
	//插入二进制数据
	bool InsertBin(XDATA kv, std::string table);
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;		
			
		}
	}
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、测试

//插入数据
    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");

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

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

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


 

你可能感兴趣的:(c++,算法,开发语言,mysql,动态链接库,数据库)