mysql / mysql.h c ++错误

时间:2012-06-29 14:04:49

标签: c++ mysql linux

这是我的用于mysql插入的c ++代码的信息。我找到了一个如何用mysql.h做的例子。程序运行正常,直到我为mysql添加以下内容。我是c ++的新手并且在其他代码中找到了相同的例子,只是不能指出我添加了mysql / mysql.h而不仅仅是mysql.h的问题,还有-i和-l来编译,如同建议通过在网上,仍然没有运气。任何帮助将不胜感激。

#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:错误:预期,或;在MYSQL之前

test.c:错误:conn未在此范围内声明

test.c:错误:conn未在此范围内声明

1 个答案:

答案 0 :(得分:2)

缺少分号:

string unknown = "Unknown";

mysql_query的调用参数太多;它不是“printf”风格的功能。您可以将sprintf用于单独的缓冲区,风险为sql injection attack。您应该使用parameterized queries