Mysql.h c ++的参数太多了

现在,当我编译我收到:

/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());