C++完成使用map Update数据 二进制数据

发布时间:2024年01月05日

?1、在LXMysql.h和LXMysql.cpp分别定义和编写关于pin语句的代码

//获取更新数据的sql语句 where语句中用户要包含where 更新
	std::string GetUpdatesql(XDATA kv, std::string table, std::string where);
std::string LXMysql::GetUpdatesql(XDATA kv, std::string table, std::string where)
	{
		string sql = "";
		if (kv.empty() || table.empty())
		{
			return "";
		}
		//update t_vedio set name= 'update001', size=1000 where id =10;
		sql = "update `";
		sql += table;
		sql += "` set ";
		for (auto ptr = kv.begin(); ptr != kv.end();ptr++)
		{
			sql += "`";
			sql += ptr->first;
			sql += "`=' ";
			sql += ptr->second.data;
			sql += "',";
		}
		//去除多余的逗号
		sql[sql.size() - 1] = ' ';
		sql += " ";
		sql += where;
		return sql;
	}

2、在LXMysql.h和LXMysql.cpp分别定义和编写UpdateBin? 插入二进制数据

    //返回更新数量,失败返回-1
?? ?int UpdateBin(XDATA kv, std::string table, std::string where);
int LXMysql::UpdateBin(XDATA kv, std::string table, std::string where)
	{
		if (!mysql || kv.empty() || table.empty())
		{
			return -1;
		}
		//update t_vedio set name= 'update001', size=1000 where id =10;
		string sql = "";
		sql = "update `";
		sql += table;
		sql += "` set ";
		MYSQL_BIND bind[256] = { 0 };
		int i = 0;
		for (auto ptr = kv.begin(); ptr != kv.end();ptr++)
		{
			sql += "`";
			sql += ptr->first;
			sql += "`=?,";
			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++;
			

		}
		//去除多余的逗号
		sql[sql.size() - 1] = ' ';
		sql += " ";
		sql += where;
		//预处理sql语句 上下文
		MYSQL_STMT* stmt = mysql_stmt_init(mysql);
		if (!stmt)
		{
			cerr << "mysql_stmt_init failed " << mysql_error(mysql) << endl;
			return -1;
		}
		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 -1;
		}
		//绑定
		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 -1;
		}
		mysql_stmt_close(stmt);

		return mysql_stmt_affected_rows(stmt);
	}

3、测试


    //更新二进制数据
    //修改id=3的图片
    XDATA udata2;
    LXData file2;
    file2.LoadFile("F:/Documents/Desktop/c++/数据库plus/src/bin/test2.jpg");
    udata2["data"] = file2;
    cout << " my.UpdateBin=" << my.UpdateBin(udata2, "t_vedio", "where id=3") << endl;
    file2.Drop();

?

文章来源:https://blog.csdn.net/wjl990316fddwjl/article/details/135401776
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。