我正在用 C++ 编写一个应用程序,它使用 MySQL 数据库来存储和收集数据。我遇到的问题是我似乎无法安装和运行 MySQL C++ 连接器。该文档非常不清楚 - IMO - 并且没有涵盖安装它的所有步骤,以便我可以将 lib 导入我的项目文件。
我已经做了什么:
- 在互联网上搜索了帮助,但没有找到有效的答案;
- 尝试手动将 lib 文件复制到/usr/local/libs/,现在工作了一半;]
- 尝试安装 Boost 库(MySQL 连接器需要),有效,但仍然出现编译错误。
所以,如您所见,我已经尝试了很多。我已经多次使用我可以在 Internet 上找到的一些示例和可能的修复程序更改了我的源代码,但似乎都没有真正起作用。您可以在下面检查我最新的代码部分。
我不知道连接器现在是否工作正常,因为它构建时没有任何错误。但是,当我执行程序时,我得到的只是以下错误:
Segmentation fault: 11
我在 Mac OS X 上构建/编译,所以我需要在该机器上安装连接器。我使用 Sublime 作为 IDE(我知道它不是 IDE,但它运行得非常好!)和 g++ 作为编译器。
另请注意,我是 C++ 编程的初学者(不理解为专家 :)),因此我可能会在我的代码或安装此连接器的过程中犯一些“巨大”错误。
感谢您的阅读,希望能听到一些有用的答案!
Qlii256
源代码:
#include <iostream>
#include "mysql_driver.h"
#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()
{
//sql::Driver *driver; // Using mysql::MySQL_Driver instead, found this fix on stackoverflow
sql::mysql::MySQL_Driver *driver;
sql::Connection *conn;
sql::Statement *stmt;
sql::ResultSet *res;
char e;
// Try a mysql db connection
try
{
driver = sql::mysql::get_driver_instance(); // create new db instance
conn = driver->connect("127.0.0.1:3306", "username", "password"); // Connect to the db
conn->setSchema("database_name"); // Select database to use
}
catch ( sql::SQLException &e )
{
cout << "MySQL error occured: " << e.getSQLState() << endl;
}
cout << "We are connected!" << endl;
return 0;
}
最佳答案
我在遇到类似问题时发现了这个老问题,也是在 Mac 上。我的解决方案是使用 brew。
brew install mysql-connector-c++
关于c++ - MySQL C++ 连接器安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876673/