Error message

Create MySQL driver for Qt5 on Windows

This tutorial explains how to create the MySQL driver for Qt5 on Windows using MinGW. If you are looking for the tutorial to do this with Qt4, click here.

Step 1

Download all the needed software.

  • Qt (libraries and source code)
    This are two separate files! E.g. qt-windows-opensource-5.0.1-mingw47_32-x86-offline.exe and qt-everywhere-opensource-src-5.0.1.zip
  • MySQL Community Server (E.g. mysql-5.5.30-win32.msi)
    I didn't use version 5.6 because the .msi only allowed me to install the 64-bit version of the Client C API Library, which apparently gives errors later in the tutorial. So make sure you get the 32-bit verison! If you really want version 5.6, an option might be to download the 32-bit version of the source files and use those. Note that I didn't test this option, so I can't help you with that.

Step 2

Install the downloaded software.

  • Install Qt, the components I installed are the default ones. (See image 1)
  • Install MySQL, you only need to install the Client C API Library the other components are optional for this tutorial. (See image 2)

    I installed it under 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

  • Extract the Qt Sources to a folder of your choosing. I placed it in the folder Qt was installed into. (C:\Qt\Qt5.0.1)
    I also renamed the extracted folder to Sources instead of the long qt-everywhere-opensource-src-5.0.1 name.
Image 1: Install QtImage 1: Install Qt
Image 2: Install MySQLImage 2: Install MySQL

Step 3

Open the Qt Command Prompt. (See image 3)
Start > All Programs > Qt 5.0.1 > 5.0.1 > MinGW 4.7 > Qt 5.0.1 for Desktop (MinGW 4.7)

Image 3: Qt Command PromptImage 3: Qt
Command Prompt

Step 4

Run the following commands (change them according to your environment):

  1. set mysql=C:\\PROGRA~2\\MySQL\\MYSQLS~1.5
  2. cd C:\Qt\Qt5.0.1\Sources\qtbase\src\plugins\sqldrivers\mysql\
  3. qmake "INCLUDEPATH+=%mysql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile mysql.pro
  4. mingw32-make

Step 5

Copy qsqlmysql.dll and qsqlmysqld.dll from C:\Qt\Qt5.0.1\Sources\qtbase\plugins\sqldrivers to C:\Qt\Qt5.0.1\5.0.1\mingw47_32\plugins\sqldrivers.

Step 6

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

Finished

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 on each of the folders.

Attachments: 

Comments

Great thanks. It works.

Cela fait 3 jours et 3 nuits que je m'arrache les cheveux avec des soucis de compatibilité, en plus des tutoriels pas clairs trouvés par ci par là......et me voilà enfin devant votre blog......en tant que professionnel, cela commencer à me coûter de piétiner pour une simple connexion à une base de données, je ne sais pas comment vous remercier. MERCI, MERCI, MERCI, MERCI, MERCI, MERCI et MILLE FOIS MERCI !!!!!!!!

N.B.: Qt 5.0.2 et MySQL Server 5.5

MySQL has always been my favorite back-end tool. I have been dealing with databases with MySQL for a long time. I loved the tutorial that you have shared to create MySQL driver for Qt5in Windows and the code snippets were worth trying.
http://www.bnschevronservice.com

thank you veeery much. worked like a charm. +1 from me :)

hi admin! first of all, thanks a lot for the precious info!

I did exactly what you said, but while executing that last command "mingw32-make" , the compilation terminates and it says: in file included from main.cpp: ../../../sql/drivers/mysql/qsql_mysql.h: fatal error: mysql.h: no such file or directory

what should I do, please?

Pages