2016-09-30 3 views
1

現在、C++を習得しています。私は、MySQLデータベースに接続する、小さな、シンプルなアプリケーションでテストを行っています。以下C++を使用しているWindows上のMySQLデータベースに接続すると、一般的な例外(不良割り当てあり)で失敗する

私の主な方法以下

int main() 
{ 
    cout << "Hello World" << endl; 
    DBConnectionManager dbConnManager; 
    dbConnManager.performSql(); 

    return 0; 
} 

私DBConnectionManagerヘッダファイル以下

#ifndef MYSQLTEST_DBCONNECTIONMANAGER_H 
#define MYSQLTEST_DBCONNECTIONMANAGER_H 

#include <stdlib.h> 
#include <exception.h> 
#include <resultset.h> 
#include <statement.h> 
#include <mysql_driver.h> 
#include <mysql_connection.h> 

using namespace sql; 

class DBConnectionManager 
{ 
private: 
    sql::Driver *driver = NULL; 
    sql::Connection *conn = NULL; 
    sql::Statement *statement = NULL; 
    sql::ResultSet *res = NULL; 
public: 
    DBConnectionManager(); 
    void performSql(); 
}; 
#endif //MYSQLTEST_DBCONNECTIONMANAGER_H 

である私のDBConnectionManagerのCPPコンストラクタ以下

#include <iostream> 
#include "DBConnectionManager.h" 

using namespace std; 

DBConnectionManager::DBConnectionManager() { 
    cout << "Starting DBConnectionManager - Updated" << endl; 
    try { 
     cout << "Getting driver instance" << endl; 
     driver = get_driver_instance(); 
     cout << "Got driver instance" << endl; 
     conn = driver->connect("tcp://127.0.0.1:3306", "root", "password"); 
     conn->setSchema("bugs"); 
     cout << "Connected to database" << endl; 
    } 
    catch (SQLException ex) { 
     cout << "Error connecting to DB: " << ex.what() << endl; 
    } 
    catch (exception ex) { 
     cout << "Something has gone wrong: " << ex.what() << endl; 
    } 
} 

は私のプログラムの出力である

Hello World 
Starting DBConnectionManager - Updated 
Getting Driver Instance 
Got Driver Instance 
Something has gone wrong: bad allocation 

ご協力いただきありがとうございます。

UPDATE

私はどこかに取得しています。私はプリプロセッサ

CPPCONN_PUBLIC_FUNC=;mysqlcppconn_EXPORTセットを持つことが必要と思った今ではある今、私は私のDBConnectionManagerにperformQuery機能を実行して成功し、私がしようとすると、今で接続し、それがエラー

をスローデータベースから読み取るように見えます

Exception thrown at 0x00007FFEEC421C80 (vcruntime140d.dll) in TestMySQL.exe: 
0xC0000005: Access violating reading location 0xFFFFFFFFFFFF` 
:それは例外ハンドラをスローしませんが、Visual Studioがエラーでポップアップ表示さ res->getString("Summary")を実行すると

void DBConnectionManager::performSql() { 
    cout << "Going to perform query" << endl; 
    try { 
     if (conn == NULL) 
     { 
      cout << "conn is null" << endl; 
      return; 
     } 
     statement = conn->createStatement(); 
     res = statement->executeQuery("SELECT * FROM reports"); 
     while (res->next()) 
     { 
      string value = res->getString("Summary"); 
      cout << "ID: " << value << endl; 
      cout << "-------------" << endl; 
      break; 
     } 

     cout << "Finished Loop" << endl; 

     delete res; 
     delete statement; 
     delete conn; 
    } 
    catch (SQLException ex) { 
     cout << "Error executing query: " << ex.what() << "Error Code: " << ex.getErrorCode() << endl; 
    } 
    catch (exception ex) { 
     cout << "Something unexpected went wrong in performSql:" << ex.what() << endl; 
    } 

を次のように0

ありがとうございました

答えて

0

最後に解決されました。

先ず、私はCPPCONN_PUBLIC_FUNC=;mysqlcppconn_EXPORTをプリプロセッサに設定する必要があると考えました。私は、get_driver_instanceが失敗した理由は何ではないかと思います。

c_str()を呼び出す必要があるため、res-> getString()を呼び出すクエリの問題がクラッシュしていました。 など。

string value = res->getString("Summary").s_ctr()

関連する問題