Mysql.h c++ 参数太多
Mysql.h c++ too many arguments
现在当我编译时,我收到:
/usr/include/mysql/mysql.h:452: error: too many arguments to function int mysql_query(MYSQL*, const char*)
mysql.h
的参数是否有限制,如果是,我该如何解决它?
#include <mysql/mysql.h>
string unknown = "Unknown";
MYSQL *conn;
conn = mysql_init(NULL);
mysql_real_connect(conn, "localhost", "root", "password", "alert", 0, NULL, 0);
mysql_query(conn, "INSERT INTO alert_tbl (alert_srcip, alert_country, alert_destip, alert_desthost, alert_destport, alert_bl) VALUES ('%s','%s','%s','%s','%s','%s')", src_ip,country_code,dest_ip,unknown,dest_prt,blip);
mysql_close(conn);
g++ test.c -o test -lstdc++ -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient
您必须使用
mysql_stmt_prepare,然后使用mysql_stmt_bind_param逐个绑定参数值
语句准备就绪后,使用mysql_stmt_execute执行它
或者使用 sprintf():
char query[1024 /* or longer */];
sprintf(query,
"INSERT INTO alert_tbl"
"(alert_srcip, alert_country, alert_destip, alert_desthost, alert_destport, "
"alert_bl) VALUES ('%s','%s','%s','%s','%s','%s')",
src_ip,country_code,dest_ip,unknown,dest_prt,blip);
mysql_query(conn, query);
或者简单地使用:
char query[1000];
snprintf(query, 1000, "INSERT INTO alert_tbl (alert_srcip, alert_country, alert_destip, alert_desthost, alert_destport, alert_bl) VALUES ('%s','%s','%s','%s','%s','%s')", src_ip, country_code, dest_ip, unknown, dest_prt, blip);
mysql_query(conn, query);
你使用它的方式,你真的把很多参数传递给mysql_query(..)
。
使用 std::stringstream 来构建查询。(警告:您需要确保它们已正确转义)。
std::stringstream ss;
ss<<"INSERT INTO alert_tbl (alert_srcip, alert_country, alert_destip, alert_desthost, alert_destport, alert_bl) VALUES ('"<<src_ip<<"','"<<country_code //and so on..
mysql_query(conn, ss.str().c_str());
在这里找到答案:
http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-prepared-statements.html
相关文章:
- C++程序给出了太多参数警告
- 调用宏时参数太多
- QObject::connect 有太多的参数
- 错误太多参数无法正常工作
- 通过引用传递太多参数可能效率低下?
- C++中的参数太多
- 提升 ODEINT 太多参数无法发挥作用
- 参数太多,无法在C++中'FILE* popen()'发挥作用
- 野牛:如何修复这个"参数太多而无法运行'int yylex()'错误?
- 在功能调用中免费结构太多参数
- C++中的模板参数太多
- 重载运算符<<:此运算符函数的参数太多
- 依赖注入/继承设计模式的构造函数参数太多
- 错误太多参数无法'void addBook()'
- 函数调用的参数太多,预期为 0,有 1。 "InfInt.h"字符串函数
- 函数调用运算符重载的参数太多
- C++中的构造函数参数太多
- Mysql.h c++ 参数太多
- 在重载函数的函数参数中使用右值引用会创建太多组合
- 警告:格式 [-Wformat-extra-args] 的参数太多