2012-11-29 11 views
4

我在嘗試運行使用C++中的MySQL連接器的編譯程序時遇到了一些問題。它編譯得很好,但是在運行它時,它會立即崩潰 - 看起來就像是要連接的線路一樣。我已經設置了所有額外的庫,依賴項,預處理器和鏈接器輸入,並且正在使用發佈解決方案配置。我正在運行Microsoft Visual Studio 2012.從Visual Studio運行MySQL Connector/C++時崩潰

我收到的錯誤如下: MyLittleSQL.exe中的0x6E69AF48(msvcr90.dll)未處理的異常:0xC0000005:訪問衝突讀取位置0x00000024。

而且調用堆棧:

MyLittleSQL.exe!main() Line 24 C++ 
MyLittleSQL.exe!__tmainCRTStartup() Line 536 C 

第24行是:

con = driver->connect("tcp://127.0.0.1:3306", "sepples_su", "easy"); 

而完整的源代碼是:

#include <stdlib.h> 
#include <iostream> 

#include "mysql_connection.h" 
#include "mysql_driver.h" 

#include <cppconn/driver.h> 
#include <cppconn/exception.h> 
#include <cppconn/resultset.h> 
#include <cppconn/statement.h> 
using namespace std; 

int main(void) 
{ 
    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", "sepples_su", "easy"); 

     /* 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; 
} 

這實際上是採取了一個例子連接器文檔,但我想確保它不是我自己的錯。

任何幫助在這裏將不勝感激,謝謝。

+1

使用Visual Studio 2012編譯連接器的DLL嗎?使用不同編譯器編譯的庫會導致類似崩潰。 –

+0

有沒有辦法檢查這個?該DLL通過mysql.com上的存檔編譯發貨。 – mtaanquist

+0

您確定您使用的是最新版本? –

回答

2

最新的C++的MySQL連接器與VC9運行時庫(Visual Studio 2008中)編譯。 Visual studio 2012使用VC11庫,所以很明顯爲什麼你的程序崩潰了。你的程序必須使用相同的運行時庫爲MySQL的C++接口:

在0x6E69AF48(MSVCR90.DLL)<

未處理的異常--- VC9

您必須使用Visual Studio 2008編譯程序,其採用VC9庫或使用Visual Studio 2012從源代碼編譯MySQL C++連接器。