SP3CH8TR
SP3CH8TR

Reputation: 123

Mysql.h c++ too many arguments

Now when I compile I recieve:

/usr/include/mysql/mysql.h:452: error: too many arguments to function int mysql_query(MYSQL*, const char*)

Is there a limit to the arguments for mysql.h and if so how do I get around it?

#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

Upvotes: 2

Views: 1188

Answers (4)

Viktor Latypov
Viktor Latypov

Reputation: 14467

You have to use mysql_stmt_prepare and then bind the parameter values one by one using the mysql_stmt_bind_param

When the statement is ready, execute it with the mysql_stmt_execute

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

Upvotes: 5

UltraInstinct
UltraInstinct

Reputation: 44444

The way you are using it, you are really passing many arguments to mysql_query(..).

Use std::stringstream to build your query. (Warning: You need to ensure they are properly escaped).

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

Upvotes: 0

user529758
user529758

Reputation:

Or simply use:

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

Upvotes: 0

Related Questions