Qt - 如何获取|编译 MySQL 驱动程序

5
我正在尝试在Qt中制作一个依赖于MySql的应用程序。经过一段时间的研究,我发现我的共享构建没有默认安装MySql驱动程序。请问如何获取或编译它(包括共享和静态)?
注意:我在Windows平台上使用Qt-4.7.2。
编辑:感谢“vrince”。他展示了这个方法,我也成功了。
 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"

就是这样。您可以在(Qt安装路径)\qt\plugins\sqldrivers中找到dll文件。


我按照@Kiran Thilak上面的指示进行了操作。在运行C:\Qt\Qt5.14.2\5.14.2\Src\qtbase\src\plugins\sqldrivers\mysql>qmake mysql.pro之后,您还需要运行以下命令:mingw32-make install。 - Njunwa Wamavoko
3个回答

14

如果你打算重新构建与MySQL关联的Qt,请停止,你不需要这样做!SQL驱动程序是插件(定义为在运行时动态加载),并且可以独立编译。

在Qt源代码树中找到驱动程序源,类似于qt/src/plugins/sqldrivers/mysql,然后进行构建。这里的关键是提供适当的MySQL开发头文件和库(客户端部分),以便驱动程序能够构建!(请注意,如果您在Windows上运行64位操作系统,可能需要32位版本的MySQL客户端)。

您可以通过qmake命令提供MySQL路径,有关方法,请参阅Anton提供的文章,个人而言,我会复制并更改.pro文件以匹配我的安装... 如果需要重新构建,则更容易。

一旦构建成功,您将拥有一个漂亮的qsqlmysql.dll文件,您必须将其复制到您用于运行应用程序的Qt目录中,通常类似于$QT_DIR中的qt/plugins/sqldrivers


8

使用MinGW32构建QT5.13 1. 下载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. 准备QT源码

>    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



构建出现错误?

  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


1

准备: 首先必须安装mingw32版本,下面是必须安装的内容

  • MariaDB连接器32位(这很重要)
  • 已安装Mingw32

构建:

  • 打开mingw32-make
  • 进入"C:\Qt\Qt5.12.9\5.12.9\src\qtbase\src\plugins\sqldrivers" (在我的驱动器中)
  • 在mingw32终端 >qmake sqldrivers.pro
  • 之后 cd mysql
  • 在mingw32终端 > qmake "INCLUDEPATH+=D:\\MariaDB\\ConnectorC\\include" "LIBS+=D:\\MariaDB\\ConnectorC\\lib\\libmariadb.lib" mysql.pro

如果出现"mysql"等错误,请按照以下方式更改mysql.pro文件

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)
  • 然后在mingw32终端中再次制作 > qmake "INCLUDEPATH+=D:\\MariaDB\\ConnectorC\\include" "LIBS+=D:\\MariaDB\\ConnectorC\\lib\\libmariadb.lib" mysql.pro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接