我在尝试在 C++ 中运行使用 MySQL 连接器的编译程序时遇到了一些问题。它编译得很好,但是在运行它时,它会立即崩溃 - 似乎在要连接的线路上如此。我已经设置了所有其他库、依赖项、预处理器和链接器输入,并且我正在使用发布解决方案配置。我正在运行 Microsoft Visual Studio 2012。
我得到的错误如下:MyLittleSQL.exe 中 0x6E69AF48 (msvcr90.dll) 的未处理异常:0xC0000005:访问冲突读取位置 0x00000024。
和调用堆栈:
MyLittleSQL.exe!main() Line 24 C++
MyLittleSQL.exe!__tmainCRTStartup() Line 536 C
第 24 行是:
con = driver->connect("tcp://127.0.0.1:3306", "sepples_su", "easy");
完整的源代码是:
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
try
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "sepples_su", "easy");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next())
{
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
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;
}
这实际上是从连接器文档中获取的示例之一,但我想首先确保这不是我自己的错。
在这里的任何帮助将不胜感激,谢谢。