2013-06-26 61 views
2

我尝试通过Eclipse中的C++连接到MySQL数据库,并使用互联网上的所有建议,但没有一个完全有帮助。我使用MinGW与Eclipse连接到MySQL。 我已经添加了:从Eclipse连接到MySQL(CDT)

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上的教程也不起作用。

+0

可以分享我整个程序的步骤我怎么能做到这一点..... 因为我遵循所有的步骤,但在执行test.exe文件失败显示' –

回答

1

刚刚替补本

driver = get_driver_instance(); 

driver = sql::get_driver_instance(); 

它只是一个命名空间的问题;

+0

我有类似的问题,所以你可以帮我请: http://stackoverflow.com/questions/39271094/unable-to-connect-mysql-database-using-cin-eclipse?noredirect=1#comment65879058_39271094 – techDigi

+0

我只是做了。希望能帮助到你 – richar8086