8

How do I load qmysql driver in Qt? I have the following code that produces these results:

("QSQLITE", "QMYSQL", "QMYSQL3") 
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3

Any suggestions on how to load it?

Noam M
  • 3,156
  • 5
  • 26
  • 41
Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112

9 Answers9

12

We should check our driver first

$ cd /opt/Qt5.2.1/5.2.1/gcc_64/plugins/sqldrivers

then we can find some files

Use the command below to check library

$ ldd libqsqlmysql.so

if you find the problem libmysqlclient_r.so.16 => not found it may be the library-dependency problem.

After I did a little research on the Internet, there is a way would be easy.

$ cd /usr/lib/x86_64-linux-gnu

if you find libmysqlclient_r.so.18,

$ cp libmysqlclient_r.so.18 libmysqlclient_r.so.16
rpax
  • 4,468
  • 7
  • 33
  • 57
Desmond
  • 121
  • 1
  • 5
  • I think you should rather make a soft link of it: ln -s libmysqlclient_r.so.18 libmysqlclient_r.so.16 – pepece Mar 25 '19 at 20:10
4

ok it worked just by copying the sqldrivers folder to my debug folder and it worked!

Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112
2

You could try diagnosing the issue with strace - it seems like the QMYSQL driver might need some run-time library dependencies to work.

qdot
  • 6,195
  • 5
  • 44
  • 95
2

On Windows (see as directory structure):

the_qt_app.exe
libmysql.dll
sqldrivers/qsqlmysql4.dll
handle
  • 5,859
  • 3
  • 54
  • 82
1

You can use QPluginLoader to get some better error message.

When I had the same problem with the MySQL driver the message was something like "The version was compiled with other options than this Qt version".

It seemed like the Qt sources, that shipped with the Qt SDK at that time, were not compatible with its binaries.

After downloading the Qt sources and compiling my own version of Qt and the MySQL driver, the problem was gone.

EDIT: Some sample code.

QPluginLoader loader;
loader.setFileName("/Users/niklaswulf/QtSDK/Qt/4.8.4/plugins/sqldrivers/libqsqlite_debug.dylib");
qDebug() << loader.load();
qDebug() << loader.errorString();

loader.setFileName("/Users/niklaswulf/QtSDK/Qt/5.0.1/5.0.1/clang_64/plugins/sqldrivers/libqsqlite_debug.dylib");
qDebug() << loader.load();
qDebug() << loader.errorString();

When compiling against 5.0.1 this is the output:

false 
"The file '/Users/niklaswulf/QtSDK/Qt/4.8.4/plugins/sqldrivers/libqsqlite_debug.dylib' is not a valid Qt plugin." 
true 
"Unknown error"

I also found the old message:

The plugin '/path/to/some/libqsqlmysql.dylib' uses incompatible Qt library. Expected build key "macosx macx-cocoa g++-4 full-config", got "macosx macx-cocoa g++-4 no-pkg-config"
Niklas
  • 249
  • 3
  • 7
1
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

Hamid Rouhani
  • 2,309
  • 2
  • 31
  • 45
1

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

Nicolas C. R.
  • 111
  • 1
  • 5
0

Here a couple of very nice links on this issue:

  1. making the plugins manually on Unix-based systems (from Qt documentation): http://qt-project.org/doc/qt-5/sql-driver.html

  2. 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

Peyman
  • 1
-2

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();
}