【问题标题】:VS Express 2013 C++ with MySQL Connector stmt->executeQuery() errorVS Express 2013 C++ 与 MySQL 连接器 stmt->executeQuery() 错误
【发布时间】:2014-07-22 07:58:17
【问题描述】:

所以我终于让 VS 2013 C++ 使用连接器 C 和 C++ 与 mySQL 连接,我知道这一点,因为我能够使用 stmt->execute() 函数将表添加到我的数据库中。

现在,我正在尝试使用 ResultSet 从函数 executeQuery 中检索行,但出现此错误:

这是我的代码:

#include <iostream>
#include <string>

#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

#define dbHOST "tcp://127.0.0.1:3306"
#define dbUSER "root"
#define dbPASS "root"
#define dbDB   "db_test"

using namespace std;
using namespace sql;

int main()
{
    sql::mysql::MySQL_Driver *driver;
    sql::Connection *con;
    sql::Statement *stmt;
    sql::ResultSet *res;

    driver = sql::mysql::get_driver_instance();
    con = driver->connect(dbHOST, dbUSER, dbPASS);

    stmt = con->createStatement();
    string query = "SELECT * FROM test";
    res = stmt->executeQuery(query.c_str());

    while (res->next()) {
        cout << "id = " << res->getInt(1); 
        cout << ", label = '" << res->getString("label") << "'" << endl;
    }

    delete res;
    delete stmt;
    delete con;

    system("pause");
    return 0;
}

在输出窗格下,它也显示如下:

First-chance exception at 0x77661D4D in MySQLConnect.exe: Microsoft C++ exception: sql::SQLException at memory location 0x0088FB5C.

Unhandled exception at 0x77661D4D in MySQLConnect.exe: Microsoft C++ exception: sql::SQLException at memory location 0x0088FB5C.

在我按下 F5 并中断后,在调用堆栈下,它显示如下:

MySQLConnect.exe!main() 第 35 行 C++

第 35 行是 res = stmt->executeQuery(query.c_str());

如果您需要有关我的代码/设置的更多信息,请告诉我。 提前致谢!

【问题讨论】:

    标签: c++ mysql sql mysql-connector


    【解决方案1】:

    我傻了,我发现了问题所在。
    我必须添加一行:

    stmt->execute("USE " dbDB);
    

    在我对该数据库进行任何进一步查询之前。 sn-p 现在是这样的:

    stmt = con->createStatement(); 
    stmt->execute("USE " dbDB);              // <-- This line, change dbDB to your own
    string query = "SELECT * FROM test";     // <- before all of these below
    res = stmt->executeQuery(query.c_str());
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      相关资源
      最近更新 更多