我们来编译配置qt的mysql数据库驱动,首先看看官方文档的说明,如下
How to Build the QMYSQL Plugin on Windows
You need to get the MySQL installation files.
RunSETUP.EXEand choose "Custom Install".
Install the "Libs & Include Files" Module. Build
the plugin as follows (here it is assumed that MySQL is installed
inC:\MySQL):
cd %QTDIR%\src\plugins\sqldrivers\mysql
qmake "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MYSQL\MySQL Server \lib\opt\libmysql.lib" mysql.pro
nmake
If you are not using a Microsoft compiler,
replacenmakewithmakein
the line above.
Note:This database plugin is not
supported for Windows CE.
Note:Including"-o
Makefile"as
an argument toqmaketo
tell it where to build the makefile can cause the plugin to be
built in release mode only. If you are expecting a debug version to
be built as well, don't use
the"-o Makefile"option.
How to build the MySQL driver for MinGW users
The following steps have been used successfully for WinXP SP3.
In this example, Qt 4.6.2 is shown.
Download the following components:
MinGW-5.1.6.exe
mingw-utils-0.3.tar.gz
Qt sources, e.g.qt-everywhere-opensource-src-4.6.2.zip
mysql-5.1.35-win32.msi
InstallMinGW-5.1.6.exein,
e.g.C:\MinGW.
Extractmingw-utils-0.3.tar.gzinto,
e.g.C:\MinGW.
Add the path forMinGW-5.1.6.exeto
yourPATHvariable,
e.g.C:\MinGW\bin;
Extract the Qt sources, (qt-everywhere-opensource-src-4.6.2.zip),
into, e.g.C:\Qt.
Add the path for the eventual Qt binary to yourPATHvariable,
e.g.C:\Qt\4.6.2\bin;.
Install MySQL (mysql-5.1.35-win32.msi), customizing the
components. Select only the headers and libraries. Install in,
e.g.C:\MySQL\MySQL51.
Open the DOS prompt, go toC:\MySQL\MySQL51\lib\opt, and run the
following commands:
reimp -d libmysql.lib
dlltool -k -d libmysql.def -l
libmysql.a
Open the DOS prompt, go toC:\Qt\4.6.2and run the following
commands:
configure.exe -debug-and-release
-platform win32-g++ -qt-sql-mysql -l mysql -I
C:\MySQL\MySQL51\include -L C:\MySQL\MySQL51\lib\opt
mingw32-make sub-src
This step takes a long time.
Open the DOS prompt, go toC:\Qt\4.6.2\src\plugins\sqldrivers\mysqland
run the following command:
qmake
"INCLUDEPATH+=C:\MySQL\MySQL51\include" "LIBS+=-L. mysql"
mysql.pro
Now the following libraries are ready inC:\Qt\4.6.2\plugins\sqldrivers.
libqsqlmysql4.a
libqsqlmysqld4.a
qsqlmysql4.dll
qsqlmysqld4.dll
To use the SDK and QtCreator directly, copy these libraries to
yourC:\Qt\...\qt\plugins\sqldrivers\, and
copyC:\MySQL\MySQL51\lib\opt\libmysql.dllto
yourC:\Qt\...\qt\bin\.
我在安装配置过程中首先参考的第二条,即"for MingW Users",但不管怎样添加环境变量总是报
" 'mingw32-make'不是内部或外部命令 "错误,将mingw32-make.exe直接拷贝到
C:\Qt\4.8.2\bin目录下报如下错误
C:\Qt\2010.02.1\bin;
C:\Qt\2010.02.1\qt\bin;
C:\Qt\2010.02.1\mingw\bin;
C:\mysql\lib\opt;
C:\mysql\include;
C:\mysql\bin
分别对应我的环境变量
C:\Qt\4.8.2\bin;
C:\Qt\4.8.2\mingw\bin(原目录不存在,新建了mingw文件夹,再将
C:\MinGW\bin文件拷贝到新建的mingw文件中);
C:\MySQL\lib(同样opt目录不存在,
因为libmysql.lib在lib文件加下,所以没有影响);
C:\MySQL\include; C:\MySQL\bin;
C:\MinGW\bin
添加完环境变量后运行 mingw32-make出现上述错误,在dos
命令行下set path查看
环境变量如下
在qt command prompt中set path查看环境变量如下
可以看到,在qt command下没有C:\MinGW\bin路径,原因不明,求解释。
在网上参考了很多资料,mingw32-make编译配置qt mysql驱动仍不成功,最后直接nmake
编译成功了。
我的nmake编译过程如下,
1
由于MySQL的默认安装路径中有空格,qmake工具无法编译,所以先把MySQL安装目录下的include和lib目录拷贝到没有空格的路径下,比如C:\MySQL(这步很重要)
2.打开Qt Command Prompt(注意是qt
command,而不是dos command,因为dos 可能无法识别qmake,nmake等
命令),输入命令:cd
%QTDIR%\src\plugins\sqldrivers\mysql按回车键,然后输入命令:
qmake “INCLUDEPATH+=C:\mysql\include”
“LIBS+=C:\mysql\lib\opt\libmysql.lib” mysql.pro,按回车键,报如下错误
WARNING: (internal):1: d backslashes are
deprecated. 这种错误的解决方法如下:
(1)进入目录E:\Qt\2010.05\qt\src\plugins\sqldrivers\mysql,用记事本打开mysql.pro,在第二行添加如下的信息:INCLUDEPATH+="C:\MySQL\include"
LIBS+="C:\MySQL\lib\opt\libmysql.lib"
(2)在dos下输入:qmake -o
Makefile mysql.pro此时可能也会提示:
WARNING: e:\Qt\2010.05\qt\src\plugins\sqldrivers\mysql\mysql.pro:2:
d backslashes are
deprecated.
WARNING: e:\Qt\2010.05\qt\src\plugins\sqldrivers\mysql\mysql.pro:2:
d backslashes are
deprecated.
WARNING: e:\Qt\2010.05\qt\src\plugins\sqldrivers\mysql\mysql.pro:2:
d backslashes are
deprecated.
但是对最后的结果没有影响。
3.nmake
4.nmake release
C:\Qt\4.8.2\src\plugins\sqldrivers\mysql\debug下的libqsqlmysqld4.a和qsqlmysqld4.dll文件拷贝到Qt安装目录\plugins\sqldrivers目录下,这样只生成了Qt在Debug模式下的MySQL驱动。在输入命令mingw-32
make
release编译Release模式下的MySQL驱动,编译成功后将Qt安装目录\src\plugins\sqldrivers\mysql\release下的libqsqlmysqld4.a和qsqlmysqld4.dll文件拷贝到Qt安装目录\plugins\sqldrivers目录下,这样就完成了Qt在Release模式下的MySQL驱动。
注意,我最终编译生成的只有qsqlmysql4.dll,
qsqlmysql4d.lib, qsqlmysql4.dll, qsqlmysql4.lib等文件,
并没有libmysqld4.a与libmysql4.a等文件,但将上述存在的4个文件拷到C:\Qt\4.8.2\plugins\sqldrivers目录下测试仍然通过,可用。
执行以上步骤后测试出现
QSqlDatabase: QMYSQL driver not loaded
将C:/MySQL/bin目录下的libmySQL.dll文件复制到Qt安装目录下的C:\Qt\4.8.2\bin目录中
之后再运行测试程序即可正确建立连接并操作数据库。
参考:
测试如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// 连接mysql测试
bool createConnection()
{
QSqlDatabase db =
QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("test");
if(!db.open())
{
QMessageBox::critical( 0,
QObject::tr("Database error"), db.lastError().text());
return false;
}
return true;
}
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
if(!createConnection())
return 1;
QSqlQuery query;
query.exec("insert into test
values(1,'test')");
return app.exec();
}
使用Mysql workbench工具查看对应数据库即可。
若报"qsqldatabase"文件找不到等错误,则
到qt的安装目录下将对应的头文件添加到include中,或者将依赖的库文件或头文件加到系统环境变量中
与项目的附加依赖库中,注意,include目录与lib目录应分别添加。
如我的相关头文件为和,打开cmd,输入set
path可查看
已有环境变量,Qt相关的为
C:\Qt\4.8.2\lib;
C:\Qt\4.8.2;
如果还不行,最笨的方法,输入完整路径,如
include
若出现“无法解析的外部符号”错误,
则在VS
2008 的 Qt 菜单下点击"qt project settings" 选项, 点击"qt
modules"标签,
勾选需添加的对应模块,即sql library即可。
参考: