使用mysql连接器构建程序时出错

时间:2016-10-22 20:27:40

标签: c++ mysql

我有以下编译时错误我找不到原因: 致命错误:mysql_connection.h:没有这样的文件或目录

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>

/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#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 'Hello World!' »AS _message'..." << 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://127.0.0.1:3306", "root", "root");
/* 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 data 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;
}

我在Codeblocks中编译它并安装了mysql连接器 我第一次尝试帮助请试试!

2 个答案:

答案 0 :(得分:2)

您很可能缺少libmysqlcppconn-dev。安装后,错误应该消失。

答案 1 :(得分:1)

我遇到了和你一样的问题,但在QtCreator的MAC上。我收到的所有内容都是:cppconn/connection.h file is not found

我还没有连接MySQL服务器。所以我评论了所有与数据库有关的文件。建设时我得到了:

library -lGL not found

因为数据库是在linux上设置的,所以我不得不更改库。所以我下载了第一个MAC的mysql库:

brew install mysql-connector-c++

然后我在.pro:

中添加了它们
mac: {
   LIBS += -framework OpenGL
   INCLUDEPATH += /usr/local/opt/mysql-connector-c++/include/cppconn
}  
编译后

我收到错误:

boost/scopped_ptr.hpp file not found

这里我需要安装boost:

brew install boost

然后在.pro中添加boost:

mac: {
   LIBS += -framework OpenGL
   INCLUDEPATH += /usr/local/opt/mysql-connector-c++/include/cppconn /usr/local/opt/boost/include
}

现在我再次出现错误:

symbol(s) not found for architecture x86_64
linker command failed with exit code 1

<强>更新

所以我的.pro在最后看起来像这样:

unix:!macx {
     LIBS += -lGL
     LIBS += -L/usr/lib -L/usr/lib -lmysqlcppconn
     INCLUDEPATH += -I/usr/include -I/usr/local/include -I/usr/local/include/cppconn
}

macx: {
    LIBS += -framework OpenGL -L/usr/local/opt/mysql-connector-c++/lib -lmysqlcppconn
    INCLUDEPATH += /usr/local/opt/mysql-connector-c++/lib
    INCLUDEPATH += /usr/local/opt/mysql-connector-c++/include/cppconn /usr/local/opt/boost/include /usr/local/opt/mysql-connector-c++/include
}

它似乎在INCLUDEPATH对于macx我需要提到缺少的库mysqlcppconn-l有助于弄清楚这是图书馆的名称。

它确实有效。现在所需要的只是与服务器的连接。