Compile/Create MySQL driver for Qt5 on Linux/Android/Windows

Create MySQL driver for Qt5 on Linux

I am working on a QT project on Ubuntu and I needed to use the MySql database driver for it. In previous versions of QT, it used to be the default installed library but with QT 2010.2 installation (4.6 and above), it is not the default installation. So, it requires to configure the MySql header files and then configure the QT to use the file. Here are the steps which worked for me. 1. First of all, we need to install the MySql Deveoper Libraries on Ubuntu. This will be required by QT to compile the MySql driver.
ubuntu 13.04
1.
sudo apt-get install libmysqlclient16-dev or libmysqlclient18-dev

2. After this installation is successful. It will create the folder “/usr/include/mysql” in the Ubuntu installation and it will also install the lib files in “/usr/lib/mysql”. We will need this to compile the QT MySql driver: 

  1. cd $QTDIR/src/plugins/sqldrivers/mysql
  2. qmake "INCLUDEPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient_r" mysql.pro
  3. make
  4. make install

3. After this, just need to compile the QT code using the “QMYSQL” driver.

Kubuntu 14.04 – x64
1.
sudo apt-get install libmysqlclient-dev

2. After this installation is successful. It will create the folder “/usr/include/mysql” in the Ubuntu installation and it will also install the lib files in “/usr/lib/x86_64-linux-gnu”. We will need this to compile the QT MySql driver: 

  1. cd $HOME/Qt/5.4/Src/qtbase/src/plugins/sqldrivers/mysql
  2. $HOME/Qt/5.4/gcc_64/bin/qmake „INCLUDEPATH+=/usr/include/mysql“ „LIBS+=-L/usr/lib/x86_64-linux-gnu -lmysqlclient_r“ mysql.pro
  3. make 
  4. make install

3. After this, just need to compile the QT code using the “QMYSQL” driver.

Create MySQL driver for Qt5 Android on Linux

Kubuntu 14.04 – x64
1.
sudo apt-get install libmysqlclient-dev
2. Download binary libmysql-driver from my github account  libmysql-android (forked from 0x-ff)
3. After this installation is successful. It will create the folder “/usr/include/mysql” in the Ubuntu installation and it will also install the lib files in “/usr/lib/x86_64-linux-gnu”. We will need this to compile the QT MySql driver: 
  1. cd $HOME/Qt/5.4/Src/qtbase/src/plugins/sqldrivers/mysql
  2. you need to export the android ndk root folder with: export ANDROID_NDK_ROOT=/home/fecub/Programme/android-ndk-r10d/
  3. ~/Qt/5.4/android_armv7/bin/qmake „INCLUDEPATH+=/usr/include/mysql“ „LIBS+=-L/home/fecub/Downloads/libmysql-android-master/android14-arm/ -lmysqlclient_r“ mysql.pro
  4. make 
  5. make install

3. After this, just need to compile the QT code using the “QMYSQL” driver.


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 Qt
Image 1: Install Qt
Image 2: Install MySQL
Image 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 Prompt
Image 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

 or

  1. cd E:\Qt\5.2.0\Src\qtbase\src\plugins\sqldrivers\mysql\
  2. qmake „INCLUDEPATH+=E:\xampp\mysql\include“ „LIBS+=E:\xampp\mysql\lib\libmysql.lib“ -o Makefile mysql.pro
  3. mingw32-make

Step 5

Copyqsqlmysql.dll and qsqlmysqld.dll from E:\Qt\5.2.0\Src\qtbase\plugins\sqldrivers or C:\Qt\Qt5.0.1\Sources\qtbase\plugins\sqldriverstoC:\Qt\5.2.0\mingw48_32\plugins\sqldrivers  or C:\Qt\Qt5.0.1\5.0.1\mingw47_32\plugins\sqldrivers(if there exist another qmysql and qmysqld overwrite it)

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. int main(int argc, char *argv[])
  4. {
  5.     QCoreApplication a(argc, argv);
  6.     qDebug() << QSqlDatabase::drivers();
  7.     return a.exec();
  8. }

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.source: Seppe MagielsFaisal’s Blog

4 Antworten zu “Compile/Create MySQL driver for Qt5 on Linux/Android/Windows

  1. It depends on your MySQL Version. I had a newer MySQL Version and qmysql.dll was compiled for an older Version.
    You can’t use a driver wich version is older then the MySQL Server. Because of this i must compile it new.

  2. Hi, thank u for this tutorial, but have error, please help

    enrike@enrike:/opt/Qt/5.4/Src/qtbase/src/plugins/sqldrivers/mysql$ /opt/Qt/5.4/gcc_64/bin/qmake „INCLUDEPATH+=/usr/include/mysql“ „LIBS+=-L/usr/lib/x86_64-linux-gnu -lmysqlclient_r“ mysql.pro
    /opt/Qt/5.4/Src/qtbase/mkspecs/features/qt_plugin.prf:50: Cannot write file /opt/Qt/5.4/Src/qtbase/mkspecs/modules-inst/qt_plugin_qsqlmysql.pri: Cannot create parent directory.
    Project ERROR: Aborting.

    Thanks.

    • I think you don’t have the rights.
      try it with sudo.
      sudo /opt/Qt/5.4/gcc_64/bin/qmake “INCLUDEPATH+=/usr/include/mysql” “LIBS+=-L/usr/lib/x86_64-linux-gnu -lmysqlclient_r” mysql.pro

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s