Http://qimo601.iteye.com/blog/1654429
1. First, go to the corresponding QT/src/plugins/sqldrivers/MySQL directory (my directory is: C: \ QT \ 4.8.0 \ SRC \ plugins \ sqldrivers \ mysql), which contains two files: MySQL. pro, a main. CPP also has a readme
2. Open the mysql. Pro file in the directory in a text editor and add the following to MySQL. Pro:
Export depath + = "C: \ Program Files \ mysql server 5.5 \ include"
Libs + = "C: \ Program Files \ mysql server 5.5 \ Lib \ libmysql. lib"
Save and exit (that is, the include path of your MySQL and the libmysql. Lib path of OPT under Lib)
3. Open QT 4.8.0 command prompt and compile the file.
# Qmake-O makefile mysql. Pro
// Three warning messages are displayed, but the alarm is not affected.
# Mingw32-make (this network has several compilation commands, I use nmake );
4. Then you will find the QT/plugins/sqldrivers path under your QT (my path is under s: \ QT \ 4.8.0 \ plugins \ sqldrivers) the following four files are added: libqsqlmysql4.a, libqsqlmysqld4.a, qsqlmysql4.dll, and qsqlmysqld4.dll (the generated file is not necessarily the above four files). Then the compilation is successful and MySQL can be used! ~
5. For security, copy the libmysql. dll file in the bin file under MySQL to system32.
#include <QtGui>#include <QtSql>#include <cstdlib>#include <QtGui/QApplication>#include <QtSql/QtSql>bool createConnection(){ qDebug() << "Available drivers:"; QStringList drivers = QSqlDatabase::drivers(); foreach(QString driver, drivers) qDebug() << "\t" << driver; QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); qDebug() << "MYSQL driver valid?" << db.isValid();}int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; if (!createConnection()) return 1; w.show(); return a.exec();}