First paste my problems, as follows:
1 |
QSqlDatabase: QMYSQL driver not loaded |
In solving this problem, first check online the various solutions are tried, but encountered some small problems which always find a solution, and ultimately on the question "Stack Overflow" got the answer, Therefore record it, again after an alternate, also hope to help to some people.
Environment: ubantu16.04 + Qt5.7 + MySql5.7
analysis
First, determine whether they well installed MySql (not installed then install)
installation method :( which also contains the link to uninstall)
http://blog.csdn.net/yimi0903/article/details/11800713
followed by executing the following command to see if these options have been installed:1
sudo apt-get install libmysqlclient-dev
Drive into the directory:
1
cd /ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers
ls view the file or folder:
1
2cc@1whispers:~/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers$ ls
libqsqlite.so libqsqlmysql.so libqsqlpsql.soRun View libqsqlmysql.so dependence:
1
ldd libqsqlmysql.so
It is shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27linux-vdso.so.1 => (0x00007ffcb64e2000)
libmysqlclient_r.so.16 => not found
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fea06463000)
libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fea0622a000)
libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007fea06011000)
libssl.so.10 => /usr/lib/x86_64-linux-gnu/libssl.so.10 (0x00007fea05dba000)
libQt5Sql.so.5 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libQt5Sql.so.5 (0x00007fea05b74000)
libQt5Core.so.5 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libQt5Core.so.5 (0x00007fea05459000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fea0523c000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fea04eb9000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fea04bb0000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fea0499a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fea045d0000)
libnss3.so => /usr/lib/x86_64-linux-gnu/libnss3.so (0x00007fea0428a000)
libnssutil3.so => /usr/lib/x86_64-linux-gnu/libnssutil3.so (0x00007fea0405d000)
libplc4.so => /usr/lib/x86_64-linux-gnu/libplc4.so (0x00007fea03e57000)
libnspr4.so => /usr/lib/x86_64-linux-gnu/libnspr4.so (0x00007fea03c18000)
libicui18n.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicui18n.so.56 (0x00007fea0377d000)
libicuuc.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicuuc.so.56 (0x00007fea033c5000)
libicudata.so.56 => /home/cc/ProgramFiles/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/./../../lib/libicudata.so.56 (0x00007fea019e2000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fea017dd000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fea015db000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fea013d3000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fea010c2000)
/lib64/ld-linux-x86-64.so.2 (0x0000560757219000)
libplds4.so => /usr/lib/x86_64-linux-gnu/libplds4.so (0x00007fea00ebd000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fea00c4d000)Here will find a problem: libmysqlclient_r.so.16 => not found, there is no explanation of this dependency, so now we have to solve is this dependence.
View all of your libmysqlclient.so
1
2
3
4
5
6cc@1whispers:~$ locate libmysqlclient.so
/home/cc/Desktop/libmysqlclient.so.20
/usr/lib/x86_64-linux-gnu/libmysqlclient.so
/ usr / lib / x86_64-linux- large column Qt5.7 not be loaded MySql driver problem (need to recompile driver ) the GNU / libmysqlclient.so.20
/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.0We found that the dependence of these.
Compile MySql driver
Note: Often this case it is advisable to look Tell me what network documentation
http://doc.qt.io/qt-5/sql-driver.html#qmysql
We must first determine their source is installed when you install Qt, if the source is not installed, you will need to reinstall Qt, remember when installed on a source that a recall election.
Into the source code in the source driver MySql
1
cd /ProgramFiles/Qt5.7.0/5.7/Src/qtbase/src/plugins/sqldrivers/mysql
Path is the path from the respective installation qt.
Compile:
qmake-> make-> the make install
execution process:1
2
3
4
5sudo /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 ,则会看到已有依赖库.
再去运行程序,则会显示成功连接数据库.1
Database connection established
这是记得要sudo make clean清理一下之前编译的中间文件.
可能出现的问题
执行qmake的时候: qmake “INCLUDEPATH+=/usr/include/mysql” “LIBS+=-L/usr/lib/mysql -lmysqlclient_r” mysql.pro这样的命令是用的默认的qmake文件,这样如果没有Qt4的SDK,则会出错,所以需要用我们自己安装Qt时路劲下的qmake.
1
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权限.
在执行make的时候,也需要用到root权限.不然会出现以下错误(我本人就是死在这里,因为没有用root权限,但是出现错误又不懂什么原因,迷茫了2天,终于解决)
1
2
3
4home/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使用证确方式(sudo make)执行后肯能会出现以下问题:
1
2
3
4
5-lQt5Sql -lQt5Core -lpthread
/usr/bin/ld: cannot find -lmysqlclient_r
collect2: error: ld returned 1 exit status
Makefile:114: recipe for target '../../../../plugins/sqldrivers/libqsqlmysql.so' failed
make: *** [../../../../plugins/sqldrivers/libqsqlmysql.so] Error 1Shows that we do not have the relevant documents lmysqlclient_r: that no libmysqlclient_r.so and other documents; but when analyzed before, know that we have
1
2
3
4
5
6
7
8
9
10
11/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 / Linux-the x86_64 - GNU / libmysqlclient.so.20.3.0
`` `
so go to link a file lmysqlclient_r related;
` `` bash
cd / usr / lib / x86_64-the GNU-Linux /
LN -s libmysqlclient.so.20.3.0 libmysqlclient_r .so.20.3.0
LN -s libmysqlclient.so.20 libmysqlclient_r.so.20
LN -s libmysqlclient.so libmysqlclient_r.soSo in order to execute sudo make, you will be successful.