Create MySQL driver for Qt on Windows

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:

After searching a while I found this solution. It works and it's fast. This is what you should do:

  1. Download the needed software/source:
    • Qt SDK (if not installed yet)
    • 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 few people mentioned in the comments, download a version that only contains the 32-bit version else you might encounter problems.
  2. 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

  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
  4. Open the Qt Command Prompt.
    Start > All Programs > Qt SDK > Desktop > Qt 4.7.3 for Desktop (MinGW)
  5. Run the following commands (change them according to your environment):
    1. set mysql=C:\\PROGRA~2\\MySQL\\MYSQLS~1.5
    2. cd C:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\mysql\
    3. qmake "INCLUDEPATH+=%mysql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile mysql.pro
    4. mingw32-make
    5. qmake "INCLUDEPATH+=%mysql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile mysql.pro "CONFIG+=release"
    6. mingw32-make
  6. 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
  7. 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. #include <QtCore/QCoreApplication>
  2. #include <QtSQL>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QCoreApplication a(argc, argv);
  7.     qDebug() << QSqlDatabase::drivers();
  8.     return a.exec();
  9. }

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.

Attachments: 

Comments

I got the same error, did u resolved it?

Best regards.

As someone written, you have chosen 64bit installation. I had the same error, and I tried to change mysql to 32bit, as someone suggested, and it fixed it. I know, that post was written in march, but maybe it will help someone else ;)

Updated the post. Thanks for the input. ;)

Hello, please, i need help,i can't compile that library if anyone can give me the compiled DLL for using with MinGW qt.

i've got already the MSVC10 dll but i want to use the mingw version.

Thank u a lot!!

Thank you very much!
I followed your article and I compiled mysql driver for Qt on windows 7 successfully!
Now my Qt program ran well.
I thought you did a good job and we should learn from you.

Sorry, I have a problem I cannot find the .pro file by different methods. Thanks a lot.

set mysql=D:\\mysql
cd D:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\mysql\
qmake "INCLUDEPATH+=%mysql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile mysql.pro
Cannot find file: mysql.pro

Hmm... Strange. Try reinstalling the Qt sources and make sure that D:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\mysql\ contains a mysql.pro file afterwards. Hope that helps.

Hello ,
I tried to the above tutorial .
When i do step 4 it gives error as 'mingw32-make' is not recognized as an internal or external command,operable program or batch file.
And also for step 6 there are no files in C:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\mysql\debug
and C:\QtSDK\QtSources\4.7.3\src\plugins\sqldrivers\mysql\release

Please help for solving this problem

Thanks
Prachi

You have to use the Qt Command prompt not the regular command prompt. ;)

Start > All Programs > Qt SDK > Desktop > Qt 4.7.3 for Desktop (MinGW)

After playing around a little with that mysql set, i finally got it to work, you have no idea how long i've been looking for an easy/working solution

Pages