How do I load qmysql
driver in Qt? I have the following code that produces these results:
(\"QSQLITE\", \"QMYSQL\", \"QMYSQL3\")
QSqlDatabase:
You could try diagnosing the issue with strace
- it seems like the QMYSQL driver might need some run-time library dependencies to work.
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3
The same problem I faced in fedora20 (64-bit) with Qt-5.2.0, and then follow steps:
$ cd /opt/Qt5.2.0/5.2.0/gcc_64/plugins/sqldrivers
$ ls
libqsqlite.so libqsqlmysql.so
Use the command below to check library dependency:
$ ldd libqsqlmysql.so
I find the problem:
libmysqlclient_r.so.16 => not found
It may be the library-dependency problem. so solve this problem:
Linking of the library file:
$ ln -s libmysqlclient_r.so.16.0.0 libmysqlclient_r.so
and again:
$ ln -s libmysqlclient_r.so.16.0.0 libmysqlclient_r.so.16
Now its work for me. All the best
Got the same problem and some Google research and intuition finally solved it. Using Qt5.9.1 and Ubuntu 17.10
First, check if the error of libmysqlclient.so.18 => not found
is present
:~/Qt5.9.1/5.9.1/gcc_64/plugins/sqldrivers$ ldd libqsqlmysql.so
Second, search where is libmysqlclient
:/$ locate libmysqlclient
Third, go to the folder where libmysqlclient is present and there make the link
:/usr/lib/x86_64-linux-gnu$ sudo ln -s libmysqlclient.so.20 libmysqlclient.so.18
and check the link made before with
ls -alh | grep libmysql
At that moment, none of those solved for me, and i decided to look further in synaptic packages, and realize that libqt5sql5-mysql
version 5.9.1 was not installed, so installing it solved the problem but i still have a message when doing ldd
./libqsqlmysql.so: /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18: version `libmysqlclient_18' not found (required by ./libqsqlmysql.so)
after that, found some links that guide me to a real solution, here are them, if you wanna know whats happening
i downloaded the library stated in the third link and worked like a charm. hope it helps!
https://www.unix.com/unix-for-advanced-and-expert-users/107611-difference-between-libsqlclient-so-libsqlclient_r-so.html
http://www.tango-controls.org/community/forum/c/general/installation/ubuntu-1604-problem-installing-from-source-code-libmysqlclient-replaces-libmysqlclient_r/
https://superuser.com/questions/1101426/installing-libmysqlclient18-on-ubuntu-16-04?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa&newreg=9c558283c488461aaf597ef1132e4ca0
My answer:
QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));
db.setDatabaseName("dbname");
db.setHostName("localhost");
db.setUserName("usernm");
db.setPassword("password");
if (db.open())
{
qDebug() << "SUCCESS!";
db.close();
}
Here a couple of very nice links on this issue:
making the plugins manually on Unix-based systems (from Qt documentation): http://qt-project.org/doc/qt-5/sql-driver.html
a very well-written link specifically on this issue (Do not forget to install the Qt from source, this can be done by either checking the corresponding (hidden) box in the first step while installing from the .run executable or by downloading the 'qt-everywhere-opensource-src' version): http://adamcavendish.is-programmer.com/posts/40431.html
Enjoy, Peyman
ok it worked just by copying the sqldrivers
folder to my debug folder and it worked!