Following the syntax from the MySQL site on the MySQL C++ connector but not having good results, here is a snippet:
#include "/usr/include/mysql++/mysql++.h"
#include "/usr/include/mysql/mysql.h"
#include "/usr/include/mysql/mysql_version.h"
#include "/usr/include/mysql_connection.h"
#include "/usr/include/mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace std;
using namespace sql::mysql;
using namespace sql;
sql::mysql::MySQL_Driver *driver;
driver = sql::mysql::get_mysql_driver_instance();
sql::Connection *con;
con = driver->connect("tcp://127.0.0.1:3306", "db", "password");
sql::PreparedStatement *prep_stmt;
prep_stmt = con->prepareStatement("INSERT INTO db(srcIP, srcCountry, destIP, destHost, destPort, blIP) VALUES (?, ?, ?, ?, ?, ?)");
prep_stmt->setString(1, src_ip);
prep_stmt->setString(2, country_code);
prep_stmt->setString(3, dest_ip);
prep_stmt->setString(4, host_name);
prep_stmt->setString(5, dest_prt);
prep_stmt->setString(6, blip);
prep_stmt->execute();
delete prep_stmt;
delete con;
}
else
sql::mysql::MySQL_Driver *driver;
driver = sql::mysql::get_mysql_driver_instance();
sql::Connection *con;
con = driver->connect("tcp://127.0.0.1:3306", "db", "password");
sql::PreparedStatement *prep_stmt;
prep_stmt = con->prepareStatement("INSERT INTO db(srcIP, srcCountry, destIP, destHost, destPort, blIP) VALUES (?, ?, ?, ?, ?, ?)");
prep_stmt->setString(1, src_ip);
prep_stmt->setString(2, country_code);
prep_stmt->setString(3, dest_ip);
prep_stmt->setString(4, he->h_name);
prep_stmt->setString(5, dest_prt);
prep_stmt->setString(6, blip);
prep_stmt->execute();
delete prep_stmt;
delete con;
Compiling with:
g++ sql.c -o sql -I/usr/include -I/usr/include/mysql -I/usr/include/mysql++ -I/usr/local/include -I/usr/local/include/cppconn -lmysqlcppconn -lmysql++ -lstdc++ -lmysqlpp -lmysqlclient
and alway get the error on the second set of MySQL Driver:
error: ‘driver’ was not declared in this scope
Based only on what I can see, it looks like you are missing braces around the statements you wish to be part of the else case. If you do this:
You will get the error on the line which reads
foo->someFunction();because the else contains only a single statement without braces and foo goes out of scope after that. You need to do this: