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 theClient 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 useC:\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 toSources
instead of the longqt-everywhere-opensource-src-5.0.1
name.
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)
Step 4
Run the following commands (change them according to your environment):
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.
-
#include <QtCore/QCoreApplication>
-
#include <QtSQL>
-
-
int main(int argc, char *argv[])
-
{
-
return a.exec();
-
}
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.
Comments
Helpful article
Great thanks. It works.
MERCI ENORMEMENT - THANKS A LOT
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
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 VERY MUCH
thank you veeery much. worked like a charm. +1 from me :)
hi admin! first of all,
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