Qt5.2 can find the mysql driver but can not load it

8.5k views Asked by At

Operating System: win7 compiler : mingw4.8(come with Qt5.2) link to : Fedora core release 5, kernel 2.6.15-1.2054_FC5 smp on an i686

    db = new QSqlDatabase();
    db->addDatabase("QMYSQL");
    db->setHostName("localhost");
    db->setDatabaseName("myclinic");
    db->setUserName("root");
    db->setPassword(",trth12");  

    qDebug() << loader.errorString();
    qDebug() << "drivers: "<< QSqlDatabase::drivers();

    if (db->open())
        qDebug() << "Success!";
    else
        qDebug() << db->lastError().text();

messages

QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL 7 “The shared library was not found.” drivers: (“QSQLITE”, “QMYSQL”, “QMYSQL3”, “QODBC”, “QODBC3”, “QPSQL”, “QPSQL7”)

“Driver not loaded Driver not loaded”

3

There are 3 answers

2
user3172211 On BEST ANSWER

Thanks for the answer, but the problem is not because I lack the qsqlmysql.dll but I don't have the libmysql.dll(looks like this dll do not come with win7).

Here is a video which show you how to install mysql

0
László Papp On

You seem to be either missing the mysql plugin on your Windows or the proper mysql installation itself in which case you will need to get that done by installing all the necessary mysql libraries on your system where the plugin QtSql driver can recognize it.

The plugin should be available under the plugins/sqldrivers folder as a dll. You could also put it into an sqldrivers folder beside your application, so somethiing like this:

- yourapplication.exe
- sqldrivers/qsqlmysql4.dll

You would need to grab it somehow, or build it yourself. This is the explanation from the documentation how to do so:

How to Build the QMYSQL Plugin on Windows

You need to get the MySQL installation files. Run SETUP.EXE and choose "Custom Install". Install the "Libs & Include Files" Module. Build the plugin as follows (here it is assumed that MySQL is installed in C:\MySQL):

cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MYSQL\MySQL Server <version>\lib\opt\libmysql.lib" mysql.pro
nmake

If you are not using a Microsoft compiler, replace nmake with make in the line above.

There is another problem with your code though. You should be using "127.0.0.1" instead of "localhost" for mysql databases.

0
user3378786 On

I had the same Pb of you

Solution is:

The Qt MySQL drivers is already inside Qt5 (but not in the previous version!)

You just need the "libmysql.dll" next to your app exe (or in windows directory!) You find "libmysql.dll" in "mysql\mysqlX.X.XX\lib\opt" directory.

If Qt MySQL driver not found the libmysql.dll the error is the same as the driver is not found...

Good chance for the next!