C++ Mysql.hc++;争论太多
现在,当我编译时,我收到:C++ Mysql.hc++;争论太多,c++,mysql,linux,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(NUL
/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);
必须使用,然后使用
当语句准备就绪时,使用
或者使用sprintf():
或者简单地使用:
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\u query(..)
使用std::stringstream构建查询。(警告:您需要确保正确转义它们)
std::stringstream-ss;
ss在这里找到了答案:
在询问以下问题之前,我可以建议您先阅读API文档吗。。。mysql_query()只允许2个参数,而您正在传递大量参数。使用sprintf进行sql注入是否安全?我查看了准备好的/参数化的语句,但似乎只有编写一整页代码才能进行插入。有一些库,如libodbc++甚至Sybase(SQL++或类似的)的语言扩展,以避免键入“整页代码”。是的,StastFf()不是安全的方式,是的,C++不是快速访问SQL(开发方面的)SQL语言。我并不是试图避免编码。我只是想学习C++,当你准备好的语句的时候,很难看到一个例子,并且理解得足够多,可以在没有更好的理解的情况下用我想做的事情来操作。C++允许使用“…”的符号使用多个(变量)参数,但MySQL的API不是这样的。这就是为什么会出现语法错误。您有两个机会:要么使用std::stringstream/sprintf()形成查询字符串,要么使用完整的prepare/execute/fetch函数集。我只是在想,6个值需要多少行代码。至少当我完成后,我会很好地掌握正在发生的事情。我会在回答中澄清这一点
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);
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());