我尝试使用Internet上的所有建议通过Eclipse中的C++连接到MySQL数据库,但是它们中没有一个完全有用。我将Eclipse与MinGW结合使用以连接到MySQL。
我添加了:
C:\Program Files\boost
C:\Program Files\MySQL\MySQL Connector C++ 1.1.3\include
C:\Program Files\MySQL\MySQL Server 5.6\include
到包含目录和
C:\Program Files\MySQL\MySQL Server 5.6\lib
C:\Program Files\MySQL\Connector C++ 1.1.2\lib\opt
到图书馆目录(-L)。我也添加了
libmysql.lib
mysqlcppconn-static.lib
到其他依赖项(-l)。
代码是:
#include <stdlib.h>
#include <iostream>
#include "mysql_driver.h"
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
using namespace sql::mysql;
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", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << 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;
}
但是它们似乎都不起作用,并且会弹出很多错误。现在我得到一个错误
"call of overloaded 'get_driver_instance()' is ambiguous"
你们是否有经验在C++环境中通过Eclipse连接到MySQL。我不是Linux用户,Linux上的教程也不起作用。 最佳答案
只是代替这个
driver = get_driver_instance();
这样
driver = sql::get_driver_instance();
这只是一个 namespace 问题;