mysql - C++ 字符串和使用 MySQL

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

我在将字符串变量传递到 MySQL 连接器时遇到问题。我正在使用 VS2015 和 MySQL Examples 中的标准示例。 下面的代码工作正常

#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”不同,因此出现了问题。

任何帮助将不胜感激。

最佳答案

以下代码更改有效。

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

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

谢谢

关于mysql - C++ 字符串和使用 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39312525/

相关文章:

php - MySQL : Monitor Queries

mysql - SQL IfNull() 函数无法识别空字符串

mysql - 最佳 Mysql 配置(分区)和索引/Hypertable/RAID 配置(大数据库)

java - 如何在 Java 中比较字符串?

c++ - 删除 std::map (Visual C++)

c++ - 如何计算图像的间距 Visual Studio

mysql - 如何对数据表的每个表值求和

c - C 中的字符串常量与 char 数组

perl - 在 Perl 中比较字符串时如何忽略重音符号?

c++ - 使用 VC++ 2008 编译引用 STLport-5.1.4 的应用程序时出现 LNK2001 错误