Reputation: 123
This is a snippit of my c++ code for mysql insertion. I found an example of how to do it with mysql.h. Program runs fine until I add in the following for mysql. I am new to c++ and have found the same examples in other code, just can't pin point the problem I have added mysql/mysql.h instead of just mysql.h and also the -i and -l to compiling as suggested through out the web and still no luck. Any help would be appreciated.
#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
test.c: error: expected , or ; before MYSQL
test.c: error: conn was not declared in this scope
test.c: error: conn was not declared in this scope
Upvotes: 0
Views: 968
Reputation: 41222
Missing semicolon:
string unknown = "Unknown";
The call to mysql_query
has too many parameters; it is not a "printf" style function. You could use sprintf to a separate buffer at the risk of sql injection attack. You should use parameterized queries.
Upvotes: 2