Mysql.h c++ 参数太多

Mysql.h c++ too many arguments

本文关键字:太多 参数 c++ Mysql      更新时间:2023-10-16

现在当我编译时,我收到:

/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