This article applies only to the Windows platform MinGW compiler
System: Windows XP
QT Version: 4.8.0
Qt Creator version: 2.4.1
Installation path:
Qt-D:\Qt\4.8.0
Qt Creator-D:\Qt\qtcreator-2.4.1
MinGW-D:\Qt\qtcreator-2.4.1\mingw
1. Configure Environment variables:
Edit path in the system variable--environment variable, properties---
;D: \qt\4.8.0\bin;d:\qt\qtcreator-2.4.1\bin;d:\qt\qtcreator-2.4.1\mingw\bin
To make the environment variable effective, if you have already opened CMD before configuring the environment variable, reopen a new cmd window.
2. Download MySQL
Official website www.mysql.com download, the file name of the non-installation package is similar to this format: Mysql-5.5.29-win32.zip
I downloaded the installation package: Http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.29-win32.zip
Just need Lib and include in MySQL, so here I provide a thin package: Mysql_lib_include.rar
Unzip to D:\
3. Download mingw-utils-0.3.tar.gz,
Extract, the bin folder inside the Reimp.exe copy to the D:\Qt\qtcreator-2.4.1\mingw\bin directory, do not copy also line, call Reimp manually add absolute path.
4. Compiling the driver
Open cmd and execute the following command:
(1) Convert libmysql format
D:
CD D:\mysql\lib
Reimp-d Libmysql.lib
Dlltool-k-D libmysql.def-l LIBMYSQL.A
(2) compiling QT mysql driver
[plain] view plain copy
CD D:\qt\4.8.0\src\plugins\sqldrivers\mysql
Qmake "Config+=release"-O Makefile "includepath+=d:/mysql/include" "LIBS+=D:/MYSQL/LIB/LIBMYSQL.A" Mysql.pro
Mingw32-make
Qmake "Config+=debug"-O Makefile "includepath+=d:/mysql/include" "LIBS+=D:/MYSQL/LIB/LIBMYSQL.A" Mysql.pro
Mingw32-make
5. Copy MySQL driver
After compiling, open the D:\Qt\4.8.0\src\plugins\sqldrivers\mysql directory, you can see the release and Debug directories, respectively, from the release and debug directory to copy the following 4 files to D:\Qt\4.8.0 \plugins\sqldrivers:
Qsqlmysql4.dll
libqsqlmysql4.a
Qsqlmysqld4.dll
libqsqlmysqld4.a
6. Copy MySQL dynamic library (important)(1) operation of the machine
Copy D:\mysql\lib\libmysql.dll files to D:\Qt\4.8.0\bin
(2) computer without MySQL
To publish an application, follow the Libmysql.dll piece to publish it with the application sibling directory.
7. Test MySQL availability
To join a MySQL service that already exists, write the following code test:
(1). Test if there is a driver
[CPP] view plain copy
Qstringlist driverlist = qsqldatabase::d rivers ();
Qdebug () << driverlist;
//Running results: ("Qsqlite", "QMYSQL3", "Qmysql", "QODBC3", "Qodbc")?
If there's qmysql and QMYSQL3, it's right.
(2). Test whether MySQL is available
[CPP] view plain copy
Qsqldatabase db = qsqldatabase::adddatabase ("Qmysql");
Db.sethostname ("192.168.1.23");
Db.setdatabasename ("Testsql");
Db.setusername ("root");
Db.setpassword ("root");
BOOL OK = Db.open ();
Qdebug () << OK;
//Run Result: true
True to indicate a successful connection
8. Provide MySQL driver for successful compilation
My version above has been written: Windows, Qt4.8, MinGW
: Qt4.8_mingw_qsqlmysql4.rar
Windows platform MinGW compiler MySQL