[ACCEPTED]-Qt - How to get|compile Mysql driver-qt

Accepted answer
Score: 14

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

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

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

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

Score: 8

Building for QT5.13 using MinGW32 1. Download 18 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 17 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 16 -> Start Menu -> Programs -> Qt 5.13.1 -> 5.13.1-> MinGW 15 7.3.0 (32-bit) -> Qt 5.13.1 (MinGW 7.3.0 14 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 13 qmake here. qmake sqldrivers.pro (to create qtsqldrivers-config.pri 12 )

    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 11 successfully build you will find qsqlmysql.dll 10 and qsqlmysqld.dll in the following location D:\QT\Qt5.13.1\5.13.1\Src\qtbase\src\plugins\sqldrivers\plugins\sqldrivers

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



Error 7 in Building?

  1. Library 'mysql' is not defined.

    In 6 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 5 path and Include path.

    Add the following 4 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 3 driver not loaded

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

Score: 1

Prepare: At first mingw32 version must be installed 2 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 1 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

More Related questions