0

Qt 项目不为 Qt SQL 模块提供 MySQL 数据库驱动程序,因此必须使用 ODBC 或手动编译插件。我正在尝试后者。我一直在使用带有 VS 2010 的预构建 Qt 4.8.1,但无法在“C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/”中找到插件的项目文件 (mysql.pro)正如许多教程使用的那样。有 qsql_mysql.pri,内容如下:

HEADERS += $$PWD/qsql_mysql.h
SOURCES += $$PWD/qsql_mysql.cpp

# modified! I added these:
#INCLUDEPATH += "C:/Program Files (x86)/MySQL/MySQL Server 5.6/include" 
#LIBS += "C:/Program Files (x86)/MySQL/MySQL Server 5.6/lib/libmysql.lib"

unix {
    isEmpty(QT_LFLAGS_MYSQL) {
        !contains(LIBS, .*mysqlclient.*):!contains(LIBS, .*mysqld.*) {
            use_libmysqlclient_r:LIBS += -lmysqlclient_r
            else:LIBS += -lmysqlclient
        }
    } else {
        LIBS *= $$QT_LFLAGS_MYSQL
        QMAKE_CXXFLAGS *= $$QT_CFLAGS_MYSQL
    }
} else {
    !contains(LIBS, .*mysql.*):!contains(LIBS, .*mysqld.*):LIBS += -llibmysql
}

构建它会导致以下错误:

release\moc_qsql_mysql.cpp(45) : warning C4273: 'QMYSQLDriver::qt_static_metacall': Inconsistent DLL-binding.
        C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'qt_static_metacall'
release\moc_qsql_mysql.cpp(57) : warning C4273: 'staticMetaObjectExtraData': Inconsistent DLL-Binding.
        C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'private: static QMetaObjectExtraData const QMYSQLDriver::staticMetaObjectExtraData'
release\moc_qsql_mysql.cpp(57) : error C2491: 'QMYSQLDriver::staticMetaObjectExtraData': Definition of static member for dllimport not valid
release\moc_qsql_mysql.cpp(61) : warning C4273: 'staticMetaObject': Inconsistent DLL-binding.
        C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'public: static QMetaObject const QMYSQLDriver::staticMetaObject'
release\moc_qsql_mysql.cpp(61) : error C2491: 'QMYSQLDriver::staticMetaObject': Definition of static member for dllimport not valid
release\moc_qsql_mysql.cpp(71) : warning C4273: 'QMYSQLDriver::metaObject': Inconsistent DLL-binding.
        C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'metaObject'
release\moc_qsql_mysql.cpp(76) : warning C4273: 'QMYSQLDriver::qt_metacast': Inconsistent DLL-binding.
        C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'qt_metacast'
release\moc_qsql_mysql.cpp(84) : warning C4273: 'QMYSQLDriver::qt_metacall': Inconsistent DLL-binding.
        C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h(104): See previous definition of 'qt_metacall'
c:\QtSDK\QtSources\4.8.1\src\sql\drivers\mysql\qsql_mysql.cpp(47) : fatal error C1083: File (Include) could not be opened: "qsqlerror.h": No such file or directory

当我尝试手动制作几乎相同的 mysql.pro 时也会发生这种情况:

LIBS += "C:/Program Files (x86)/MySQL/MySQL Server 5.6/lib/libmysql.lib"
INCLUDEPATH += "C:/Program Files (x86)/MySQL/MySQL Server 5.6/include/"

SOURCES += "C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.cpp"
HEADERS += "C:/QtSDK/QtSources/4.8.1/src/sql/drivers/mysql/qsql_mysql.h"

可能是更接近原始版本的版本。

关于这种方法的许可的另一个问题:使用 Qt 源构建此插件是否需要发布使用的源和/或使用模块作为动态库是否存在限制(Qt 开源不允许用于商业目的的静态链接)。

4

1 回答 1

0

我走错路了!文件 mysql.pro 存在于 C:\QtSDK\QtSources\4.8.1\src\plugins\sqldrivers\mysql

TARGET = qsqlmysql

# added these two lines after copying from
# C:\Program Files (x86)\MySQL\MySQL Server 5.6
LIBS += lib/libmysql.lib
INCLUDEPATH += include/

SOURCES = main.cpp
include(../../../sql/drivers/mysql/qsql_mysql.pri)

include(../qsqldriverbase.pri)

现在我可以构建插件了。我将文件复制到 C:\QtSDK\Desktop\Qt\4.8.1\msvc2010\plugins\sqldrivers

以管理员身份运行 MySQL

"C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin\mysqld.exe" --defaults-file="C:\ProgramData\MySQL\MySQL Server 5.6\my.ini"

但即使我将 DLL 复制到 Qt 的插件目录中

C:\QtSDK\Desktop\Qt\4.8.1\msvc2010\plugins\sqldrivers

他们没有被发现:

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC
QSqlError(-1, "Driver not loaded", "Driver not loaded") 

这个答案Qt 4.8.0 - MySQL Driver not listed有解决方案:

要使用它,只需确保您将找到 libmysql.dll 的路径(通常是 C:\mysql\bin)作为环境变量 PATH 的一部分。

或者只是将 DLL 复制到 Qt 项目目录根目录中。

于 2013-08-26T15:08:20.850 回答