【发布时间】: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