You are here

Create MySQL driver for Qt on Windows

Posted on 08 February 2012 - 16:08

As you might have seen I'm working on the E-Sheet project of the Royal Belgian Ice Hockey Federation (RBIHF). This project is created with Qt on a Windows system and requires a connection to a MySQL database. Since Qt doesn't deliver the MySQL driver by default, I had to created it myself. You can recognize the problem from the following error: Driver not loaded.

I checked out several tutorials but they were either unsuccessful or extremely slow or even both. The main reason they were unsuccessful is that they aren't compatible with the new Qt versions (4.7.x and up). I listed a few of them below:

Qt Documentation
This one should work, but will take a lot of time, since you have to compile Qt. Another problem was that while configuring Qt the following error occurred: Perl not found in environment - cannot run syncqt. A solution for this error can be found here.

MySQL Community Server (E.g. mysql-5.5.20-win32.msi) Make sure it is the 32-bit version, because the 64 bit version isn't supported yet.Update: As a fewpeople mentioned in the comments, download a version that only contains the 32-bit version else you might encounter problems.

Now there are three possibilities.

Qt SDK and Qt Sources are already installed. Continue to Step 3.

Qt SDK is installed but Qt Sources isn't.

Open Maintain Qt SDK this can be found at Start > All Programs > Qt SDK > Maintain Qt SDK

Select the Qt Sources you want to install and follow the wizard.

Continue with Step 3.

Qt SDK and Qt Sources aren't installed.

Install Qt SDK and don't forget to install the Qt Sources as well. Otherwise go to the step above.

Once everything is installed continue with Step 3.

I installed the Qt SDK in C:\QtSDK and this path will be used below, so remember it. You should also remember the version of the Qt Sources, here it's 4.7.3

Install MySQL. You only need to install the Client C API Library the other components are optional for this tutorial. I installed it in C:\Program Files (x86)\MySQL\MySQL Server 5.5. Since the path contains spaces, you should use C:\PROGRA~2\MySQL\MYSQLS~1.5, else it won't work. 1

Copy the following files to C:\QtSDK\Desktop\Qt\4.7.3\mingw\plugins\sqldrivers

libqsqlmysqld4.a and qsqlmysqld4.dll from C:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\mysql\debug

libqsqlmysql4.a and qsqlmysql4.dll from C:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\mysql\release

Copy libmysql.dll from %mysql%\lib to C:\Windows

That's all, but don't forget to add QT += sql to your project file, else it won't work. To check which drivers are available run this program. You can also download the code as a Qt project in the attachments.

1 Note: For 32-bit systems the install path of MySQL will be C:\Program Files\MySQL\MySQL Server 5.5 and the path without spaces will be C:\PROGRA~1\MySQL\MYSQLS~1.5. To determine a custom path without spaces, use the dir /x command.

First of all it's a warning not an error, which is a good thing. The warning informs us that we should escape the backslashes, because the unescaped version will be unsupported in the future. To fix this you should replace 1 and 3 of the fifth step by the following:

evry thing is all right (the copilation were fine)
but it dosen't want to work
qsqlmysql4.dll and qsqlmusqld4.dll are in qt\4.8.1\plugins\sqldrivers the two other .a too eihter mysql.dll is in 4.8.1\bin

First of all, I'm going to assume you mean libmysql.dll when you say mysql.dll, correct me if I'm wrong. :) If this is the case the file shouldn't be copied into the qt\4.8.1\bin folder but into C:\Windows. The original file can be found in the %mysql%\lib folder.

ye ur right i mean libmysql.ddl when i said mysql.dll
and i copyed this file into c:\windows, c:\qt\4.8.2\bin and i affected in "PATH" variable
no positive result
can i do the work with QtSDK v1.2 cause im working with QTSDK1.1...TANKS MAN

If you want to build the program on the other system just compile the driver on that system. I don't know why you can't use it exactly, but I guess it's because the paths of Qt, MinGW and MySQL are different on the machine.

If you want to deploy (run) theprogram on the other system, copy the dll's into the same folder as the executable. The MySQL driver, and other sql-drivers, have to be placed in a subfolder called "sqldrivers". An example of the application folder structure:

This is an indicator of an incorrect path to MySQL. Make sure the path is correct, you can do this by copying the path you're using for the %mysql% variable and paste it into the address bar of Windows Explorer. If you get an error the path is incorrect, else you will go to the folder where MySQL is installed.

i'm sorry but i have the same problem,it tells me that can't find the mysql.h .but i am sure the MySQL path is correct and without spaces(for me ,i change it to "C:\MySQL" ) and this is my errors:
Setting up environment for Qt usage...

When using this method, it worked perfectly when making the .ddl and .a files, however, Qt was still not working with the driver. My problem was that even with the line Qt += sql in my .pro file, the libraries weren't being included. I had to copy all of the lib files in C:\QtSDK\Desktop\Qt\4.7.3\mingw\plugins\sqldrivers to C:\QtSDK\Desktop\Qt\4.8.1\mingw\lib. I also had to include these lines in my .pro file:

I have follow all the step as you mention. but it could'nt working. I generated release and debug .a and .dll files and put it into your given path and download your project also but its not loading mysql driver.

Hey! I'd like to thank you!
Your post is precise and it worked perfectly!
It is necessary to pay attention to details!
It worked with Qt 4.8.1, MySQL 5.5 (32 bit) on my Win 7 64 bit machine.
Thanks a lot!

thanks for your help works fine with Qt 4.7. I'm currently downloaded the new Qt 5.0 Open Source Version with MingW but i don't know how to compile the MySQL Drivers for the qt 5 environment. I can not find an QtSDK from 5.0. Qt 5.0 comes only with sqlite drivers :( Any idea how to fix this?

i just created an Install Script which installs "Qt Creator 5.0.1" (if not already installed), installs the necessary MySql C-Library (Silent Installation, if not already installed), and also creates the Sources folder (If not already installed). After this process the Scripts compiles and copies the DLL Files in the necessary directories.

I will upload this Installation Files on my Server, and if you allow, i can post the URL over here?! Of cause the Code is included into the Zip File, so everyone can configure the Script how he needs.

It didn't worke for me my mysql path is in C:\Program Files\MySQL\MySQL Server 5.6 and i change it to
set mysql c:\\PROGRA~1\\MySQL\\MYSQLS~1.6
and wrote these instruction:
cd C:\QtSDK\QtSources\4.8.1\src\plugins\sqldrivers\mysql\

Hey Seppe,
Thanks very much for all your input on this. For me its turning out te be the last straw. I would very much appreciate some help: The problem is: my .a files are being created as expected in both folders, but the .dll's fail. I get big amount of lines like:

Never mind, I got it working! For everyones informations: The errors had to do with it being a 64bit version that gtot installed. (for some reason, downloading the MySQL msi for BOTH 64 and 32, and choosing the architecture during install, still made it 64bit at some level. Even though I'm sure I choose 32bit.)After changing it to 32bit by doing a manual(zip archive) install for 32bit only, was I succesfull. Then all I had to add was
QT += sql
to the .pro file to get it working.
Still: thnx for doing a great service to the community with this blog.
Cheers