Creating MySQL Driver for Qt5 on Windows

Home / Creating MySQL Driver for Qt5 on Windows

Creating MySQL Driver for Qt5 on Windows

December 9, 2015 | Article | No Comments

This article will discuss on how to create MySQL driver for Qt4 on Windows using MinGW. This article is motivated by the fact that MySQL driver isn’t shipped by default on Qt5.

All the works are done using:

  1. Windows 8
  2. MinGW 4.8.1 (TDM)
  3. Qt 5.1.1
  4. MySQL 5.6.14.0

Actually my Windows 8 is 64-bit, but all program we test here is 32-bit version (MinGW32, MySQL 32-bit).

Obtain Materials

For this article we need Qt libraries, Qt source code, and MySQL server.

Qt version I use is 5.1.1. You need to download two separate files (libraries and source code).

We also need MySQL server and the library. The version I use is 5.6.14 which you can download here.

The reason why I didn’t use the 64-bit version simply because my current project should produce 32-bit app. If you want to follow this article, make sure all the materials are 32-bit.

Installation the Materials

Installing Qt

Install Qt libraries. Installing default components would do alright. Although you can use full installation, but in this article we will use the default one. I select drive C:\ like default path and leave it to Qt. At the end, the Qt is installed as C:\Qt\Qt5.1.1

Next, extract the Qt sources to a folder you choose. Let’s extract it to the same folder as Qt libraries does. Which means, the target path is C:\Qt\Qt5.1.1. Rename the long name “qt-everywhere-opensource-src-5.1.1” to “sources”. Thus, we have C:\Qt\Qt5.1.1\sources.

Installing MySQL

Install MySQL. Select custom installation. At minimal, you need to install Client C API Library, the other components are optional. Let’s installed it under C:\Program Files (x86)\MySQL\MySQL Server 5.6

Building & Deploying

Open Qt command prompt, You should have one. It is like normal command prompt, except it has default Qt environment variable we need. Once you open it, you will be brought to your Qt directory.

Now execute following:

set mysql=C:\\PROGRA~2\\MySQL\\MYSQLS~1.6
cd C:\Qt\Qt5.1.1\sources\qtbase\src\plugins\sqldrivers\mysql\
qmake "INCLUDEPATH+=%mysql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile mysql.pro
mingw32-make

Once it is built successfully without error, you will have some fileson C:\Qt\Qt5.1.1\sources\qtbase\plugins\sqldrivers. They are:

  1. libqsqlmysql.a
  2. libqsqlmysqld.a
  3. qsqlmysql.dll
  4. qsqlmysqld.dll

For .dll files, move them to C:\Qt\QT5.1.1\5.1.1\mingw48_32\plugins\sqldrivers.

For .a files, move them to C:\Qt\Qt5.1.1\5.1.1\mingw48_32\lib

Also, copy libmysql.dll from %mysql%\lib to C:\Windows

Testing

To use the driver, don’t forget to add QT += sql to project file, else it don’t work.

Check which drivers are available by this code:

#include <QtCore/QCoreApplication>
#include <QtSql>

int main() {
    QCoreApplication a(argc, argv);
    aDebug() << QSqlDatabase::drivers();
    return a.exec();
}

, ,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial