gpt4 book ai didi

c++ - 无法从mysql读取结果

转载 作者:行者123 更新时间:2023-11-29 05:10:45 25 4
gpt4 key购买 nike

我使用 visual c++,我已经链接到 libaries c++ 连接器(MySQL 连接器 C++ 1.1.7)和 boost(boost_1_61_0)。

我为 mysql 使用 32 位连接器,64 位连接器根本不起作用。

我有 windows 10(64 位)

它编译。<​​/p>

但是当调试器到达 cout << res->getString(1) << endl; 行时它崩溃了该示例取自 https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html

代码:

#include <stdlib.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(void)
{
cout << endl;
cout << "Running SELECT * from cars" << endl;


try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;

/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://localhost:3306", "root", "*****");
/* Connect to the MySQL test database */
con->setSchema("sakila");

stmt = con->createStatement();
res = stmt->executeQuery("SELECT * from cars");
while (res->next()) {

cout << res->getString(1) << 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;
}

cout << endl;

return EXIT_SUCCESS;
}

错误信息:

Unhandled exception at 0x01327EA6 (msvcp120d.dll) in cppMySql.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC

最佳答案

在查看了我在评论中提到的原始解决方案后,我设法重现了您的情况并发现我遇到了完全相同的错误。这就是为我解决的问题:

cout << res->getString(1).c_str() << endl;

关于c++ - 无法从mysql读取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39725444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com