0
votes

I've just done developing a QT CRUD Application. I am using SQLite to store the information locally in my disk where the application is running. This way my application works fine.

But the app lack the the a central database server. Therefor, I started by installing MySql-server and MySql-workbench. I imported my DB in the workbench and made sure the server is running.

Now on Qt, I wrote the following lines to conenct to my server on the "localhost " using port "3306" as follow:

db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName(databaseName);
db.setUserName(userName);
db.setPassword(userPass);

qDebug() << appDataPath;

if (!db.open()){
    return db.lastError().text();
}

However, I get the following error on QT "Application Outout ":

QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7

I tried to make sure that libmysql.dll is located in "C:\Program Files\MySQL\MySQL Server 5.7\lib" ..

I copied it also to "C:\Users\Abubakr\Documents\GitHub\DEBUG\build-Muwassa-Desktop_Qt_5_4_2_MinGW_32bit2-debug"

but it is not working at all.

Please help !!

2
Did you build Qt with mysql support? It's not enabled by default.drescherjm
Is C:\Program Files\MySQL\MySQL Server 5.7\lib in your system path?drescherjm
If possible, a tutorial on how to connect Qt on windows to Mysql server will help a lot That is off topic for StackOverflow.drescherjm
@drescherjm : what do you mean ? I installed Qt long time ago, then installed MySql. I don't understnad how can I build Qt myself with mysql support ?! ..... and yes, the lib folder is stated in my system path variable.... Sorry also about asking for tutorial, I agree with yo u (I'll remove the request)McLan

2 Answers

0
votes

You need to build the QMYSQL driver by yourself first.

When you download Qt you have also the source that are provided with it.

In my case, it is in : C:\Qt\5.9\Src\qtbase\src\plugins\sqldrivers\mysql Here you have a *.pro (that is a kind of makefile generator). You just have to open it with QtCreator, Compile it and you will get a dll. This dll is the MySQL driver and must be in the same dirent of your application.

To do that, if you have a problem with the compilation (like mysql.h not found), you must "install" mysql library also.

In Linux you can have the same issue as well.

0
votes

It's late to answer, but this may be useful for future readers.

When QMYSQL is available but not loaded, you should find libqsqlmysql.a and libqsqlmysqld.a files corresponding to qsqlmysql.dll and qsqlmysqld.dll files and copy them (.a files) into the \mingw73_xx\lib folder. (replace xx by 64 or 32)

Note that for project kit 64 bit use MySql 64 bit, and for project kit 32 bit use MySql 32 bit.

Details are available at QT : QSqlDatabase: QMYSQL driver not loaded [WINDOWS]