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 插入二进制数据
//返回更新数量,失败返回-1int 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();