不能从mysql读取结果
Cant read result from mysql
我使用visual c++,我已经链接到库c++连接器(MySQL连接器c++ 1.1.7)和boost (boost_1_61_0)。
我使用32位连接器mysql, 64位连接器不工作。
我有windows 10(64位)
它编译。
但是当调试器到达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++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 读取一组用户输入,按升序排序,然后打印结果
- 硬编码字符串与强制转换为 PUCHAR 并打印到控制台时从控制台读取的字符串的结果不同
- 尝试以随机字符(长读取)打印字符串返回结果
- 从文本文件中读取并使用 sizeof 结果计算整数的数量到 nan
- C++ 读取逗号与不需要的结果分开
- 多线程文件读取为每个线程生成相同的结果
- MySQL C++ 不读取结果
- 为什么 protobuf 只读取最后一条消息作为输入结果
- 使用fstream从二进制文件中读取并将结果存储在向量中
- 正在读取C++中的二进制文件,并将结果输出为hexdump
- 从文件中读取结果数据,加载它,比较它,然后打印显示
- 执行控制台程序,写入标准输入并使用管道读取结果
- 读取文本文件并在C/C++中每行只得到最后一个整数的结果
- 是否可以从readelf命令中读取结果
- 使用boost(C++库)读取执行命令的结果
- 从文件中读取的浮点值与计算结果不一致
- 读取文件行和查询结果的编程函数
- 读取文件和使用getline进行标准输入的结果不同
- 最后的文件行读取会产生不同的结果