Download MySQL C link library
Address: http://dev.mysql.com/downloads/
Select Download according to current system:
Second, install the downloaded MySQL driver file.
Note that it must be installed in a file that does not contain Chinese, white space, and an outstanding symbol, for the sake of respect, I installed directly in the E:\MySQL directory, it is recommended to do so.
Be sure to select the second item, no we can not specify the installation directory, will be installed by default under the C drive. But it doesn't matter where you install it later, but here is a bit of knowledge about installing MySQL because you can choose the installation path in this way when you install MySQL server.
The installation directory is as follows:
Third, library file copy
My QT installation directory is E:\QT4\4.8.4 and the version number is 4.8.4 so open the following directory:
E:\QT4\4.8.4\src\plugins\sqldrivers\mysql, the contents are as follows:
Enter the Lib directory under the MySQL driver installation directory:
Copy the Libmysql.dll and Libmysql.lib files to the
Under the E:\QT4\4.8.4\src\plugins\sqldrivers\mysql directory:
Iv. Formal Compilation Drive
Edit the Mysql.pro file in the directory and add:
includepath+= "E:\MySQL\include"
Libs+=-le:\mysql\lib\-llibmysql
The directory in the circle in the diagram is the MySQL driver installation directory, this must not be written wrong.
Run Qt's console window to compile the file. The QT console opens in the following ways:
The console is as follows:
Switch to the E:\QT4\4.8.4\src\plugins\sqldrivers\mysql directory,
Input: CD E:\QT4\4.8.4\src\plugins\sqldrivers\mysql
Executive Qmake
Perform Mingw32-make debug to generate the debug version of the link library:
Successful build:
Execute mingw32-make releaseto generate the release version of the link library:
Successful build:
The following files are generated under the E:\QT4\4.8.4\src\plugins\sqldrivers\mysql project directory:
Enter the Debug and release directories separately, and you will see the generated
Qsqlmysqld4.dll , Qsqlmysqld4.lib, and Qsqlmysql4.dll, Qsqlmysql4.lib linked library files.
Copy these 4 files to the E:\QT4\4.8.4\plugins\sqldrivers directory:
Copy the Libmysql.dll and Libmysql.lib files that were previously copied to the project directory to the C:\Windows\System32 directory.
This way the MySQL drive side is fully installed complete. The above compilation process can also be done with Qt Creator, no difference from compiling a common QT project.
V. Test connection to MySQL
Below do a test program, new control QT System project, main.cpp file content as follows:
Source:
1#include <QCoreApplication>2#include <QDebug>3#include <QString>4#include <QStringList>5#include <QtSql/QtSql>6#include <QtSql/QSqlDatabase>7 8 9 intMainintargcChar*argv[])Ten { One qcoreapplication A (argc, argv); A -Qdebug () <<"Available Drivers:"; - theQsqldatabase Db=qsqldatabase::adddatabase ("Qmysql"); - -Db.sethostname ("127.0.0.1"); -Db.setport (3306); +Db.setdatabasename ("MySQL"); -Db.setusername ("Root"); +Db.setpassword ("Qaz"); A at if(Db.open ()) { -Qdebug () <<"success to link mysqld db!"; - } - - returna.exec (); -}
The results of the operation are as follows:
This proves that MySQL installation is complete, enjoy!
Reference: http://jingyan.baidu.com/article/27fa7326ffb99146f8271fe0.html