Mysql.h c ++参数太多了

时间:2012-06-29 14:27:41

标签: c++ mysql linux

现在我编译时收到:

/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

4 个答案:

答案 0 :(得分:5)

您必须使用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);

答案 1 :(得分:0)

或者只是使用:

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

答案 2 :(得分:0)

您使用它的方式,您实际上是将许多参数传递给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());

答案 3 :(得分:0)