Page 1 of 1

Qt: Building QMYSQL with MinGW Rate Topic: -----

#1 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Posted 10 January 2011 - 01:31 AM

What is QMYSQL?
QMYSQL is the driver needed by the Qt SQL classes for connecting to MySQL database.

With this article I will be guiding you through building QMYSQL with (and for) MinGW. You may find this helpful even if you aren't using MinGW (or even windows for that matter).

Let's begin
Before we begin, note that the documentation for MinGW users at is horrible.
And so I have used the following links as a guide:
2) this blog

Tools required
1) Qt SDK: It comes with MinGW. I am using Qt version 4.7.1 installed at C:/Qt (and not C:/Qt/2010.05).

2) MinGW: You may already have this installed either from the Qt installation or from MinGW at sourceforge. I use the MinGW distro at since it is updated quite frequently.

If you haven't installed MinGW using the Qt installer then make sure C:/MinGW/bin has been added to the PATH environment variable.

3) reimp: If you don't have this exe file in MinGW/bin (that's Qt/mingw/bin for Qt installed MinGW), then get it from sourceforge. reimp is part of mingw-utils. Currently it is at MinGW/Utilities/mingw-utils/.
Download mingw-utils-0.3.tar.gz since version 0.4 isn't going to work for our purpose.
Copy all the files to MinGW/bin.

4) MySQL Headers and Libraries: Download MySQL community server.
Here I will be using MySQL 5.1 (Unfortunately MSI Installer Essentials - 39 - MB doesn't have libmysql.dll. So you will have to download the other version - 106 MB).
Install the C headers and libraries and change path to C:\MySQL\MySQL51 (My aim here is to avoid spaces in the installation path like "Program Files").
Attached Image

Step 1: Open command prompt (If you have installed MinGW through Qt installer, then open the Qt command prompt from the start menu and skip step 2).

Step 2:
set PATH=C:\MinGW\bin;C:\Qt\bin;C:\Qt\qt\bin
set QTDIR=C:\Qt\qt
set QMAKESPEC=C:\Qt\qt\mkspecs\win32-g++

QMAKESPEC is the path to the directory containing qmake.conf.
At Qt\qt\mkspecs you can see the configurations for various platforms and compilers. We are setting it to win32-g++.

Step 3:
set mysqldir=C:/MySQL/MySQL51

Set the path according to your installation. If you have spaces in your installation path, then step 5 might be a problem. Say that you have installed MySQL at the default location at C:/Program Files/MySQL/MySQL Server 5.1 then
setting to C:/Progra~1/MySQL/MySQLS~1.1 might work.

Note: Use forward slashes.

Step 4:
cd %mysqldir%/lib/opt
reimp -d libmysql.lib
dlltool -k -d libmysql.def -l libmysql.a

Now you should have libmysql.a and LIBMYSQL.def at %mysqldir%/lib/opt

cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake -o Makefile "INCLUDEPATH+=%mysqldir%/include" "LIBS+=%mysqldir%/lib/opt/libmysql.a"

You will get a lot of junk on screen but hopefully no errors. Check this.

Writing Code

STEP 1: Add Qt += sql to your project's .pro file.
STEP 2: Add some code to your project.
qDebug() << QSqlDatabase::drivers();

This should give you output similar to ("QSQLITE", "QMYSQL3", "QMYSQL", "QODBC3", "QODBC").

Add more code (modify it accordingly):
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");

if(ok) {
    QSqlQuery query;
    query.exec("SELECT * FROM faculty");
    while ( {
       int FID = query.value(0).toInt();
       QString FName = query.value(1).toString();
       qDebug() << FID << FName;

STEP 3: Now to make sure your project finds libmysql.dll (which is at C:\MySQL\MySQL51\lib\opt).
You can follow do one of the following 3 methods:
1) Copy the dll to C:/Windows

2) Before the above test code add:
QLibrary *pl=new QLibrary(QString("K:/Users/Mogral/Documents/Qt/first/libmysql.dll"));

QLibrary::load() should return true on successful loading of dll.
Use pl->unload() to unload dll.

3) Adding LIBS+=-LK:\MySQL\MySQL51\lib\opt -lmysql to the .pro file.
The advantage here is that you can use relative path and easily distribute the software without "installing" the dll to the windows folder.

Hope that helped.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1