代码
mysql官网上的2个例子有些问题, 这里上面根据参考资料稍作了修改,能正常完成mysql功能;
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "xxx", "xxxxxx");
/* Connect to the MySQL test database */
stmt = con->createStatement();
stmt->execute("use hbbdata");
res = stmt->executeQuery("SELECT * FROM userinfo;");
while (res->next()) {
cout << "\t... MySQL replies: "<<endl;
/* Access column data by alias or column name */
cout << res->getInt("userno")<<endl;
cout << res->getString("name") << endl;
cout << res->getInt("age")<<endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
编译
g++ -o test test.cpp -lmysqlcppconn