Reputation: 100
I'm trying to create a Qt interface to connect to a MySQL database but it's giving me an error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
this is my code:
QSqlDatabase db1 = QSqlDatabase::addDatabase("QMYSQL");
db1.setHostName("localhost");
db1.setUserName("root");
db1.setPassword("");
db1.setDatabaseName("journal_machine");
This is my configuration:
Upvotes: 1
Views: 4453
Reputation: 176
It's late to answer, but this may be useful for future readers.
For project kit 64 bit use MySql 64 bit, and for project kit 32 bit use MySql 32 bit.
note: xx: 64 or 32
qsqlmysql.dll
and qsqlmysqld.dll
files should be exist in <QtDir>\mingw73_xx\plugins\sqldrivers
folder.
libqsqlmysql.a
and libqsqlmysqld.a
files should be exist in <QtDir>\mingw73_xx\lib
folder.
libmysql.dll file should be exist in <QtDir>\mingw73_xx\bin
folder.
Now, you can build your project.
// comment:
If "QMYSQL driver not loaded" error persists, follow instrument:
Copy MySql folder from Program Files to a simple path. (for example: "D:/MySQL")
Go to "\Src\qtbase\src\plugins\sqldrivers\mysql" folder
Open mysql.pro as text.
Comment QMAKE_USE += mysql
line using '#'. (#QMAKE_USE += mysql
)
Add following lines after that:
LIBS += 'D:/MySQL/lib/libmysql.lib'
INCLUDEPATH += 'D:/MySQL/include'
DEPENDPATH += 'D:/MySQL/include'
Save file.
Open Qt 5.13.1 (MinGW 7.3.0 xx-bit).exe
from start menu (for example).
Go to <QtDir>\Src\qtbase\src\plugins\sqldrivers
folder (use 'cd' command).
Use qmake mysql.pro
command.
Use mingw32-make
command.
If compile done successfully, go to <QtDir>\Src\qtbase\src\plugins\sqldrivers\plugins\sqldrivers
folder.
Copy qsqlmysql.dll
and qsqlmysqld.dll
files into <QtDir>\mingw73_xx\plugins\sqldrivers
folder.
Copy libqsqlmysql.a
and libqsqlmysqld.a
files into <QtDir>\mingw73_xx\lib
folder.
// comment:
If error persists, follow instrument:
<MySQL Connector C 6.1 Dir>\bin
folder.libmysql.dll
file into <QtDir>\mingw73_xx\bin
folder.Upvotes: 1