While using Qt, I faced the problem where I could not get the QMysql working. It will keep giving me the following error:
QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
I checked out some threads that might give me a lead on solving this issue. I tried all the methods mentioned in this thread but none worked.
I found another lead, which was building the driver file from source and this is introduced in the official Qt resources. I will not write about how to download the source code of a given Qt version.
BTW, my environment is Ubuntu 14.04 (64bit) and have installed Qt5.7 and Qt5.8 through offline installer. Thus, I have /opt/Qt5.7 and /opt/Qt5.8.
Here I will log my attempt to follow the tutorial:
the guide says that the below are the commands that I must used to build the .so file that will allow me to work with mysql.
$ cd ~ $ cd Downloads/qt-everywhere-opensource-src-5.8.0/qtbase/src/plugins/sqldrivers/mysql $ qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro $ make
However, I ran into a few errors here
error1) Qt 5.7 5.8 version mismatch
I went into the 5.8.0 version source and executed the qmake command. But it gave me an error like this:
$ qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro Project ERROR: Could not find feature framework. Project ERROR: Could not find feature c++11.
I found that this error is probably due to the Qt version mismatch. As you can see below, the qmake tool is using Qt 5.7.0. One could make modifications so that the qmake tool will use Qt 5.8.0 but I simple decided to download the Qt 5.7.0 source package and run the sequence above.
$ qmake --version QMake version 3.0 Using Qt version 5.7.0 in /opt/Qt5.7.0/5.7/gcc_64/lib
okay, now that I have changed to the Qt 5.7.0 source package, I hoped that the
qmake .... command would work this time. However I ran into an error like below:
$ qmake "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib -lmysqlclient_r" mysql.pro $ make make: Nothing to be done for `first'.
As you can see I have ran into an error in the
make command. The reason behind this error was that I had already done this before writing this post and haven’t cleaned it. If you are doing this for the first time, then you probably won’t run into this problem.
This can be cleaned with
$ make clean rm -f .moc/moc_qsql_mysql_p.cpp rm -f .moc/main.moc rm -f .obj/main.o .obj/qsql_mysql.o .obj/moc_qsql_mysql_p.o rm -f *~ core *.core
now that we’ve clean the build, redoing the ‘make’ command should show proper building logs. And in the end, it will show a message something like below:
$ make /opt/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_... ... g++ -Wl,--no-undefined -Wl,-O1 -Wl,--enable-new-dtags -Wl,-z,origin -Wl,-rpath,\$ORIGIN/../../lib -Wl,-rpath,\$ORIGIN/../../lib -shared -o libqsqlmysql.so .obj/main.o .obj/qsql_mysql.o .obj/moc_qsql_mysql_p.o -L/usr/local/lib -L/usr/lib64/mysql -lmysqlclient_r -lz -lcrypt -lnsl -lm -lssl -lcrypto -L/opt/Qt5.7.0/5.7/gcc_64/lib -lQt5Sql -lQt5Core -lpthread mv -f libqsqlmysql.so ../../../../plugins/sqldrivers/
In the last line you can see that the built .so file has been moved to some other directory. Go to the directory and you will see the freshly built .so file. ( you don’t need to do this since you have once more step left in the original build directory)
$ cd ../../../../plugins/sqldrivers/ $ lslibsqlmysql.so
back to the build directory( sourcepackagedir/qtbase/src/plugins/sqldrivers/mysql) , the final is the
make install command as introduced in the Qt official guide. However, you need to do this with sudo.
$ sudo make install install -m 755 -p ../../../../plugins/sqldrivers/libqsqlmysql.so /opt/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/libqsqlmysql.so strip --strip-unneeded /opt/Qt5.7.0/5.7/gcc_64/plugins/sqldrivers/libqsqlmysql.so install -m 644 -p /home/chadrick/Downloads/qt-everywhere-opensource-src-5.7.0/qtbase/lib/cmake/Qt5Sql/Qt5Sql_QMYSQLDriverPlugin.cmake /opt/Qt5.7.0/5.7/gcc_64/lib/cmake/Qt5Sql/
as you can see, the built .so file will be moved to its appropriate location.
I think after this, you will not run into the
QMYSQL driver not loaded error.
Installed Ubuntu mate. The QMYSQL not found error can be solved by installing a package:
$ sudo apt install libqt5sql5-mysql