Qt - How to get|compile Mysql driver

24,850

Solution 1

If you plan to rebuild Qt linked to MySQL you can stop now you don't have to ! SQL drivers are plugins (by definition dynamically loaded at run time) and can be compiled independently.

Find the driver sources in the Qt source tree somthing like qt/src/plugins/sqldrivers/mysql then build it. The game here is to provide the proper MySQL development headers and libraries (client ones) so that the driver will build ! (Be aware if you are one windows it may be 32bits version of MySQL client you need even if you are running a 64bits OS).

You can provide MySQL path via the qmake command for that refer to the article given by Anton, personally I copy and change the .pro file to match my installation ... easier to rebuild later if needed.

Once the build succeeded, you will have a nice qsqlmysql.dll you must copy into the Qt dir you use to run you apps basically something like qt/plugins/sqldrivers in the $QT_DIR.

Solution 2

Building for QT5.13 using MinGW32 1. Download MySql C Connector v6.1.

>   Download the MySql Installer from:
>   <https://dev.mysql.com/downloads/installer/>
>   Install C Connector 6.1 (Note the location we will need it later)

2. Getting QT Source Ready

>    To build a plugin for QT u need to get its source. You can install it from Maintenance Tool or manually get it from github repository.
  1. Building Plugin

    1. Open MinGW CMD (Windows -> Start Menu -> Programs -> Qt 5.13.1 -> 5.13.1-> MinGW 7.3.0 (32-bit) -> Qt 5.13.1 (MinGW 7.3.0 32-bit) )

    2. cd to sqldrivers path in qt source

    cd D:\\QT\\Qt5.13.1\\5.13.1\\Src\\qtbase\\src\\plugins\\sqldrivers
    
    1. Run qmake here. qmake sqldrivers.pro (to create qtsqldrivers-config.pri )

    2. cd to mysql.

    cd D:\\QT\\Qt5.13.1\\5.13.1\\Src\\qtbase\\src\\plugins\\sqldrivers\\mysql
    
    1. Run qmake here. qmake mysql.pro
  2. Add the plugin to the list

    Once successfully build you will find qsqlmysql.dll and qsqlmysqld.dll in the following location D:\QT\Qt5.13.1\5.13.1\Src\qtbase\src\plugins\sqldrivers\plugins\sqldrivers

    Copy both qsqlmysql.dll and qsqlmysqld.dll and place them in the compiler’s plugin directory D:\QT\Qt5.13.1\5.13.1\mingw73_32\plugins\sqldrivers



Error in Building?

  1. Library 'mysql' is not defined.

    In file cd D:\QT\Qt5.13.1\5.13.1\Src\qtbase\src\plugins\sqldrivers\mysql\mysql.pro

    Commend the line QMAKE_USE += mysql

  2. Adding Library path and Include path.

    Add the following to mysql.pro at end

    LIBS += -L'C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/lib/'
    -llibmysql
    
    INCLUDEPATH += 'C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/include'
    
    DEPENDPATH += 'C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/include'
    
  3. QSqlDatabase: QMYSQL driver not loaded

    Add the .dll files from C:/Program Files (x86)/MySQL/MySQL Connector C 6.1/lib to D:\QT\Qt5.13.1\5.13.1\mingw73_32\bin

Solution 3

Prepare: At first mingw32 version must be installed list bellow

  • MariaDB connector 32 bit (this is important)
  • Mingw32 installed

Build:

  • open mingw32-make
  • go to "C:\Qt\Qt5.12.9\5.12.9\src\qtbase\src\plugins\sqldrivers" (in my Drive)
  • in mingw32 terminal >qmake sqldrivers.pro
  • after that cd mysql
  • in mingw32 terminal > qmake "INCLUDEPATH+=D:\\MariaDB\\ConnectorC\\include" "LIBS+=D:\\MariaDB\\ConnectorC\\lib\\libmariadb.lib" mysql.pro

If you get an error like "mysql" etc. change the mysql.pro file as below

TARGET = qsqlmysql

HEADERS += $$PWD/qsql_mysql_p.h
SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp
#QMAKE_USE += mysql
INCLUDEPATH +='D://MariaDB//include//mysql'(Check files if there isn't)
DEPENDPATH += 'D://MariaDB//include//mysql'
LIBS += -L'D://MariaDB//lib//libmysql.lib' 
-llibmysql

OTHER_FILES += mysql.json

PLUGIN_CLASS_NAME = QMYSQLDriverPlugin
include(../qsqldriverbase.pri)
  • then make again in mingw32 terminal > qmake "INCLUDEPATH+=D:\\MariaDB\\ConnectorC\\include" "LIBS+=D:\\MariaDB\\ConnectorC\\lib\\libmariadb.lib" mysql.pro
Share:
24,850
prabhakaran
Author by

prabhakaran

A newbie to qt,openGL,NetworkProgramming Know python,perl,shell scripting. A well versed(medium level) C++,linux,perl,c# programmer Crawling around javascript,xul and new technologies. I am a Linux lover got tricked into windows.

Updated on July 05, 2022

Comments

  • prabhakaran
    prabhakaran almost 2 years

    I am trying to make a MySql-dependent app in Qt. After some time I came to know that my shared-build is not having MySql driver(by default). Can Anybody say how to get|compile it (both in shared and static).

    Note: I am using Qt-4.7.2 in Windows platform

    EDIT: Thanks to "vrince". This is the way he showed => I did.

     1)Open qt-command prompt 
    
     2)Goto (Qt's installation path)\qt\src\plugins\sqldrivers\mysql
       in my case:
       D:\TempInstallationFolder\Qt\dynamic-New\qt\src\plugins\sqldrivers\mysql
    
     3)qmake
    
     4)make 
       or
       mingw32-make (provided your PATH variable contains "(Qt installation path)\mingw\bin")
    
       ("make" should work if you didn't mess up with path variables. It in turn
        invokes mingw32-make. I messed up this a little bit. So I invoked 
        mingw32-make directly.)
    
     5)In the above  command you have to indicate the mysql's "lib" path, 
       and "include" path through the compile flag options. Or Add those lines
       in the pro file like below
    
       INCLUDEPATH += "C:\Program Files\MySQL\MySQL Server 5.1\include"
       LIBS += -L"C:\Program Files\MySQL\MySQL Server 5.1\lib\opt"
    

    That's it. You can find the dlls in (Qt-installation path)\qt\plugins\sqldrivers