- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在学习 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/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!