2016-09-04 1 views
1

MySQLコネクタに文字列変数を渡す際に問題が発生しています。私はVS2015とMySQLの例の標準例を使用しています。 私は、文字列変数のdbを使用して、以下のようにMySQLのコネクタに渡ししようとしています、私のファイルにデータベーススキーマをハードコーディングされないようにするため、以下のコードは、しかし細かいC++の文字列とMySQLを使用する

#include "stdafx.h" 
#include <iostream> 
#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() 
{ 

cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl; 
try { 
    sql::Driver *driver; 
    sql::Connection *con; 
    sql::Statement *stmt; 
    sql::ResultSet *res; 
    // Create a connection 

    //string db = "test"; 
    driver = get_driver_instance(); 
    con = driver->connect("tcp://127.0.0.1:3306", "user", "user"); 
    // Connect to the MySQL test database // 
    con->setSchema("test"); 
    //con->setSchema(db); 
    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").c_str() << endl; 
     cout << "\t... MySQL says it again: "; 
     // Access column fata by numeric offset, 1 is the first column //  
     cout << res->getString(1).c_str() << 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; 
} 
getchar(); 
return 0; 
} 

の作品メモリ位置0x000000F08CAFCDA0ではstd :: bad_alloc:マイクロソフトC++の例外:こんにちはWorld.exeで0x00007FFCBA981F28で

未処理の例外として、実行時に未処理の例外で失敗したコード

#include "stdafx.h" 
#include <iostream> 
#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() 
{ 

cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl; 
try { 
    sql::Driver *driver; 
    sql::Connection *con; 
    sql::Statement *stmt; 
    sql::ResultSet *res; 
    // Create a connection 

    string db = "test"; 
    driver = get_driver_instance(); 
    con = driver->connect("tcp://127.0.0.1:3306", "user", "user"); 
    // Connect to the MySQL test database // 
    //con->setSchema("test"); 
    con->setSchema(db); 
    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").c_str() << endl; 
     cout << "\t... MySQL says it again: "; 
     // Access column fata by numeric offset, 1 is the first column //  
     cout << res->getString(1).c_str() << 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; 
} 
getchar(); 
return 0; 
} 

私はMySQLが不正に切断されていることがわかります。したがって、MySQLがスキーマとして渡された文字列を理解できないと仮定しています。

私は、変数dbのエンコード/ターミネーションが "テスト"とは異なると思われます。

ご協力いただきますようお願い申し上げます。

答えて

1

次のコード変更が行われました。

con->setSchema(db.c_str()); 

私はそれがstd :: stringとsql :: SQLstringの間の変換で問題だったと思います。

ありがとうございました

関連する問題