C ++字符串并使用MySQL

时间:2016-09-04 00:30:19

标签: mysql string visual-c++ visual-studio-2015 c++14

我在将字符串变量传递给MySQL连接器时遇到问题。我正在使用VS2015和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;
}

但是,由于我不想在我的文件中硬编码数据库模式,我试图使用字符串变量db并将其传递给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;
}

在运行时因未处理的异常而失败

Hello World.exe中0x00007FFCBA981F28处的未处理异常:Microsoft C ++异常:内存位置为0x000000F08CAFCDA0的std :: bad_alloc。

我可以看到MySQL正在断断续续地断开连接因此我假设MySQL无法理解作为模式传递的字符串。

我怀疑变量db中的编码/终止与“test”不同,因此存在问题。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

以下代码更改有效。

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

我想这是std :: string和sql :: SQLstring之间的转换问题。

由于