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

user3172211 picture user3172211 · Jan 9, 2014 · Viewed 8.4k times · Source

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”

Answer

lpapp picture lpapp · Jan 9, 2014

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.