2

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

/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

4 回答 4

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);
于 2012-06-29T14:32:54.357 回答
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);
于 2012-06-29T14:35:49.437 回答
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());
于 2012-06-29T14:37:28.433 回答
0

在这里找到了答案:

http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-prepared-statements.html

于 2012-10-16T21:41:18.043 回答