ubuntu下安装qt的mysql驱动遇到的问题

参照网上教程,做的过程遇到很多问题,记下.


1.首先我将软件中心搜到的mysql server和客户端都装上了.


2.sudo apt-get install libmysqlclient18 libmysqlclient-dev libmysqld-dev


3.参照qt creater的帮助,搜sql可以找到database driver字样


How to Build the QMYSQL Plugin on Unix and Mac OS X
You need the MySQL header files and as well as the shared librarylibmysqlclient.so. Depending on your Linux distribution you may need to install a package which is usually called "mysql-devel".
Tell qmake where to find the MySQL header files and shared libraries (here it is assumed that MySQL is installed in /usr/local) and runmake:
cd $QTDIR/src/plugins/sqldrivers/mysql qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro make
After installing Qt, as described in the Installing Qt for X11 Platformsdocument, you also need to install the plugin in the standard location:
cd $QTDIR/src/plugins/sqldrivers/mysql make install


于是 cd ~/work/Qt&Opencv_src/qt-everywhere-opensource-src-4.8.3/src/plugins/sqldrivers/mysql

qmake "INCLUDEPATH+=/usr/include/mysql"  "LIBS+=-L/usr/lib/mysql/plugin -lmysqlclient_r" mysql.pro  


出现错误:Failure to read QMAKESPEC conf file  qt-everywhere-opensource-src-4.8.3/mkspecs/linux-g++/ qmake.conf

我就觉得很奇怪,因为我已经设置了环境变量,并且测试过可以编译成功,这时发现 qmake读取的不是mkspecs/linux-g++/ qmake.conf(qt库安装路径的qmake.conf),而是源码包的配置文件,于是在 ~/.profile添加一句
 export QMAKESPEC=$QTDIR/mkspecs/linux-g++
这下可没错了.而接下去make的时候又出错了
 
main.cpp:42:30: fatal error: qsqldriverplugin.h: No such file or directory
搜遍所有地方确实没有那个头文件,于是意识到了,也许是因为我的mysql server是软件中心安装,而不是源码包编译安装,很多东西包括头文件之类都没装上.因为make出错,接下去就不能进行make install,也就不会产生所说的libqsqlmysql.so
咋办呢,要么remove mysql相关东西并重新编译安装.


搜着搜着,又有人说可以直接sudo apt-get install libqt4-sql-mysql  或者本来这东西就已经随系统安装了,然后将libqsqlmysql.so复制一份到$QTDIR/plugins/sqldrivers/ 下面,但在/usr/lib/qt4目录下却没找到这个动态库,直接find,发现居然存在于

/usr/lib/i386-linux-gnu/qt4/plugins/sqldrivers/libqsqlmysql.so 接下去的就不用我多说了吧.


做个demo测试一下,建立控制台程序,记得在pro文件中 QT +=那里加上sql,否则qmake是不会去找sql的相关部分的。先在mysql建立一个study数据库。

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
#include <QtCore/QCoreApplication>
#include <QDebug>
#include <QSqlDatabase>

int main( int argc,  char *argv[])
{
    QCoreApplication a(argc, argv);
    QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL");
    db.setHostName( "localhost");
    db.setDatabaseName( "study");
    db.setUserName( "root");
    db.setPassword( "666666");
     if(!db.open())
    {
        qDebug() <<  "Unable to open database";
    }
     else
    {
        qDebug() <<  "Database connection established";
    }
     return a.exec();
}
输出如图:

ubuntu下安装qt的mysql驱动遇到的问题_第1张图片

终于成功了,yeah~~~

你可能感兴趣的:(ubuntu下安装qt的mysql驱动遇到的问题)