gpt4 book ai didi

c++ - 使用 C++ 在 Windows 上连接到 MySQL 数据库在一般异常下连接失败,分配错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:24 27 4
gpt4 key购买 nike

我目前正在学习 C++,但进展不是很顺利。我正在使用一个连接到 MySQL 数据库的小型简单应用程序进行测试。

下面是我的主要方法

int main()
{
cout << "Hello World" << endl;
DBConnectionManager dbConnManager;
dbConnManager.performSql();

return 0;
}

下面是我的DBConnectionManager头文件

#ifndef MYSQLTEST_DBCONNECTIONMANAGER_H
#define MYSQLTEST_DBCONNECTIONMANAGER_H

#include <stdlib.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
#include <mysql_driver.h>
#include <mysql_connection.h>

using namespace sql;

class DBConnectionManager
{
private:
sql::Driver *driver = NULL;
sql::Connection *conn = NULL;
sql::Statement *statement = NULL;
sql::ResultSet *res = NULL;
public:
DBConnectionManager();
void performSql();
};
#endif //MYSQLTEST_DBCONNECTIONMANAGER_H

下面是我的 DBConnectionManager cpp 构造函数

#include <iostream>
#include "DBConnectionManager.h"

using namespace std;

DBConnectionManager::DBConnectionManager() {
cout << "Starting DBConnectionManager - Updated" << endl;
try {
cout << "Getting driver instance" << endl;
driver = get_driver_instance();
cout << "Got driver instance" << endl;
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
conn->setSchema("bugs");
cout << "Connected to database" << endl;
}
catch (SQLException ex) {
cout << "Error connecting to DB: " << ex.what() << endl;
}
catch (exception ex) {
cout << "Something has gone wrong: " << ex.what() << endl;
}
}

下面是我的程序的输出

Hello World
Starting DBConnectionManager - Updated
Getting Driver Instance
Got Driver Instance
Something has gone wrong: bad allocation

更新

我要去某个地方。我想我需要在预处理器中设置 CPPCONN_PUBLIC_FUNC=;mysqlcppconn_EXPORT

现在它似乎已成功连接,所以现在当我尝试从数据库中读取时会抛出错误

现在我在我的 DBConnectionManager 中运行一个 performQuery 函数,如下所示

void DBConnectionManager::performSql() {
cout << "Going to perform query" << endl;
try {
if (conn == NULL)
{
cout << "conn is null" << endl;
return;
}
statement = conn->createStatement();
res = statement->executeQuery("SELECT * FROM reports");
while (res->next())
{
string value = res->getString("Summary");
cout << "ID: " << value << endl;
cout << "-------------" << endl;
break;
}

cout << "Finished Loop" << endl;

delete res;
delete statement;
delete conn;
}
catch (SQLException ex) {
cout << "Error executing query: " << ex.what() << "Error Code: " << ex.getErrorCode() << endl;
}
catch (exception ex) {
cout << "Something unexpected went wrong in performSql:" << ex.what() << endl;
}

当它运行 res->getString("Summary") 时,它不会抛出异常处理程序,但 Visual Studio 弹出错误:

Exception thrown at 0x00007FFEEC421C80 (vcruntime140d.dll) in TestMySQL.exe:
0xC0000005: Access violating reading location 0xFFFFFFFFFFFF`

最佳答案

终于解决了。

我首先想到我需要在预处理器中设置 CPPCONN_PUBLIC_FUNC=;mysqlcppconn_EXPORT。结果我不知道,这就是 get_driver_instance 失败的原因。

调用 res->getString() 的查询问题崩溃了,因为您需要调用 c_str()。例如

string value = res->getString("Summary").s_ctr()

关于c++ - 使用 C++ 在 Windows 上连接到 MySQL 数据库在一般异常下连接失败,分配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39798222/

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