8 August 2012

Build MySQL Plugin for Qt(4.7.3) on Windows

The QtSql module uses driver plugins to communicate with the different database APIs. Since Qt's SQL Module API is database-independent, all database-specific code is contained within these drivers. Several drivers are supplied with Qt and other drivers can be added. The source for building QMYSQL plugin is provided on the qt repository and also along with the qt-everywhere-opensource archive. Here we'll see how to build the QMYSQL plugin using installed MySQL libraries.

Follow the given steps to build the plugin -
1. Download the following softwares/sources.
      a) QtSDK (if not installed) from http://qt.nokia.com/downloads.
      b) My SQL Community Server (e.g. - mysql-5.5.20-win32.msi) from http://dev.mysql.com/downloads/mysql/. Make sure it is 32 bit version as 64 bit is not supported yet.
      c) Qt-Everywhere-Opensource zip file so that you may have the complete plugin source. This is optional as you can download selected source later using QtSDK package manager.

2. Mostly, Qt Sources are not installed with the QtSDK, so you'll have to install the sources before proceeding. To install the sources -
     a) Open 'Maintain Qt SDK' program from start menu.
     b) Select the Qt Sources you want to install and follow the wizard.

Remember to mark the correct version of Qt Sources.

3. Install the downloaded MySQL. Install it in C:\MySql or any other location without any space in between.  Providing library path creates problem when there is space in between so better avoid it.

4. After installation is complete you are ready to build plugin. There are two ways in which you may proceed.
   a)   Run the following commands in command prompt. Change them according to you environment.
         i)   set mysql=c:\\mysql\\MySQL_Server_55
         ii)  cd C:\QtSDK\QtSources\4.7.4\src\plugins\sqldrivers\mysql\
         iii) qmake "INCLUDEPATH+=%sql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile        mysql.pro
         iv) mingw32-make
         v)  qmake "INCLUDEPATH+=%sql%\\include" "LIBS+=%mysql%\\lib\libmysql.lib" -o Makefile mysql.pro "CONFIG+=release"
         vi) mingw32-make

   b) If the above method doesn't work, you can compile the plugin using the QtCreator. Go to the project location (i.e. C:\qt-everywhere-opensource-src-4.8.2\qt-everywhere-opensource-src-4.8.2\src\plugins\sqldrivers\mysql, if you have extracted it from qt-everywhere-opensource or C:\QtSDK\QtSources\4.7.4\src\plugins\sqldrivers\mysql\, if you have installed sources from Maintain Qt SDK) and open the mysql.pro file in QtCreator.
      i) You have to add mysql installed library to your project. To do this right click on the project and click on Add Library.
      ii) Add the library as shown in the images below - Initially select the External library option.

Then add the mysql library path -
Click on the next after specifying the path as above. Finally this library will be added to the pro file.

Now compile the project. This will generate the output as libqsqlmysqld4.a and libqsqlmysqld4.dll.

c) Copy above two files to C:\QtSDK\Desktop\Qt\4.7.4\mingw\plugins\sqldrivers from both debug as well as release build.

d) Copy libmysql.dll from MySql installed folder to C:\Windows folder.

Now you are ready to code using sql plugin.

Compile this qt code to check if the MySql plugin has been detected successfully.

#include <QtCore/QCoreApplication>
#include <QtSQL>
#include <QDebug>

int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);
    return a.exec();

Note - Don't forget to add QT += sql to your project file.