Qt5.7不能加载MySql驱动问题.(需要重新编译驱动)
转自:http://blog.csdn.net/qq_28851503/article/details/52422302
首先贴上我遇到的问题,如下:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
Unable to open database
在解决这个问题的时候,首先在网上查询了各种解决方案,都尝试过,但是其中遇到的一些小问题,总是找不到解决方法,最终在"Stack Overflow"上提问得到了答案,故此记录一下,一遍以后备用,也希望能帮助到一些人.
环境: ubantu16.04 + Qt5.7 + MySql5.7
分析
安装方法:(该链接里面同时也包含卸载方法)
2. 进入驱动所在目录:
cd /ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers
3. ls 查看目录里面的文件:
cc@1whispers:~/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers$ ls
libqsqlite.so libqsqlmysql.so libqsqlpsql.so
4. 执行命令查看 libqsqlmysql.so 的依赖:
ldd libqsqlmysql.so
显示如下:
在此会发现一个问题: libmysqlclient_r.so.16 => not found,说明没有这个依赖项,所以现在我们要解决的是这个依赖.
5.查看自己所有的 libmysqlclient.so
cc@1whispers:~$ locate libmysqlclient.so
/home/cc/Desktop/libmysqlclient.so.20
/usr/lib/x86_64-Linux-gnu/libmysqlclient.so
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.0
可知到我们的依赖有这些.
编译MySql驱动
1.首先要确定自己在安装Qt的时候安装了源码,如果没有安装源码,则需要重装Qt,切记装的时候记得选上源码那一项.
2.进入到源码中MySQL驱动的源码中
cd /ProgramFiles/Qt5.7.0/5.7/Src/qtbase/src/plugins/sqldrivers/mysql
路径是各自安装qt的路径而来.
3. 执行编译:
qmake->make->make install
执行过程:
sudo /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/bin/qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro
sudo make
sudo make install
在查看依赖 ldd libqsqlmysql.so ,则会看到已有依赖库.
再去运行程序,则会显示成功连接数据库.
Database connection established
这是记得要sudo make clean清理一下之前编译的中间文件.
可能出现的问题
1.执行qmake的时候: qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro这样的命令是用的默认的qmake文件,这样如果没有Qt4的SDK,则会出错,所以需要用我们自己安装Qt时路劲下的qmake.
sudo /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/bin/qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro
需要加上sudo,不然会出现 not permit的错误,即没有权限,所以需要用root权限.
2.在执行make的时候,也需要用到root权限.不然会出现以下错误:
注: 我本人就是死在这里,因为没有用root权限,但是出现错误又不懂什么原因,迷茫了2天,终于解决.
home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/bin/moc -DQT_NO_MTDEV -DQT_NO_LIBUDEV -DQT_NO_TSLIB -DQT_NO_LIBINPUT -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_NO_EXCEPTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG
-DQT_PLUGIN -DQT_SQL_LIB -DQT_CORE_LIB -I/home/cc/ProgramFiles/Qt5.7.0/5.7/Src/qtbase/mkspecs/linux-g++ -I/home/cc/ProgramFiles/Qt5.7.0/5.7/Src/qtbase/src/plugins/sqldrivers/mysql -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtSql/5.7.0 -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtSql/5.7.0/QtSql
-I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtCore/5.7.0 -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtCore/5.7.0/QtCore -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtSql -I/home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/include/QtCore
-I/usr/include/c++/5 -I/usr/include/x86_64-linux-gnu/c++/5 -I/usr/include/c++/5/backward -I/usr/lib/gcc/x86_64-linux-gnu/5/include -I/usr/local/include -I/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed -I/usr/include/x86_64-linux-gnu
-I/usr/include main.cpp -o .moc/main.moc
moc: Cannot create .moc/main.moc
Makefile:652: recipe for target '.moc/main.moc' failed
make: *** [.moc/main.moc] Error 1
3. 使用证确方式(sudo make)执行后肯能会出现以下问题:
可知,我们没有了lmysqlclient_r相关的文件:即没有libmysqlclient_r.so等文件;但是在之前分析的时候,知道我们有
/home/cc/Desktop/libmysqlclient.so.20/usr/lib/x86_64-linux-gnu/libmysqlclient.so/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.0
所以要去链接出lmysqlclient_r相关的文件;
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">cd </span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">/</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">usr</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">/</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">lib</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">/</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">x86_64</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">linux</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">gnu</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">/</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;"> ln </span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">s libmysqlclient</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="lit" style="color: rgb(125, 39, 39); margin: 0px; padding: 0px; border: 0px;">20.3</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;"><span style="color: rgb(125, 39, 39);">0</span></span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;"> libmysqlclient_r</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="lit" style="color: rgb(125, 39, 39); margin: 0px; padding: 0px; border: 0px;">20.3</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;"><span style="color: rgb(125, 39, 39);">0</span></span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;"> ln </span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">s libmysqlclient</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="lit" style="color: rgb(125, 39, 39); margin: 0px; padding: 0px; border: 0px;">20</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;"> libmysqlclient_r</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="lit" style="color: rgb(125, 39, 39); margin: 0px; padding: 0px; border: 0px;">20</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;"> ln </span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">-</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">s libmysqlclient</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so libmysqlclient_r</span><span class="pun" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">.</span><span class="pln" style="color: rgb(48, 51, 54); margin: 0px; padding: 0px; border: 0px;">so</span></code>
这样在去执行sudo make命令,则会成功.
----------------------------------------------------------------------------------------------------------------------
注:
大家在用这个篇文章的时候若不能解决问题,也希望能在评论中给出,我们一起讨论解决.
转载请注明出处!
----------------------------------------------------------------------------------------------------------------------