Compiling Qt MySQL database driver on Windows

Source: Internet
Author: User

Let's talk about it first. It took nearly one day to complete it. In fact, it was about a year ago, but it was not recorded at that time, so now I have to do it again, it can be seen how important a blog is! It took so much time to compile the source code. As we all know, it is really hard to compile a Qt source code. You cannot shut down the computer for a long time !!! Okay, let's talk about it!

For the most recent project, select the Qt and MySQL combinations. I have been working in linux for a long time. Unfortunately, the environment is Windows, so I have to build a new environment. First, install the development environment of Qt. I directly select the QtSDK and install a package. MySQL also downloads the latest installation package on the official website. The two parts have been tested separately, and they all work normally! Next, you need to use Qt to operate MySQL. This requires the MySQL database driver. After the QtSDK is installed, it only comes with the SQLite database driver and needs to be compiled by yourself. The compilation tool I selected is the well-known MinGW, and I need the Qt source code package. The version I selected is 4.7.4.

The next step is to compile the file. You 'd better take a look at the help documentation of Qt. For how to use the help documentation of Qt, see the previous blog, the search results in the help document are as follows:

This help document describes in detail how to compile the drivers of various databases. Here we choose to compile the MySQL database driver in Windows.

The document is very detailed. It is basically a step. I have translated the document. You can see it at a Glance:

 

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 pathMinGW-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, goC: \ MySQL \ MySQL51 \ lib \ opt, And run the following commands:

    • Reimp-d libmysql. lib
    • Dlltool-k-d libmysql. def-l libmysql.
  • Open the DOS prompt, goC: \ 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, goC: \ 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 \.

Here I will post the files compiled by myself. I am familiar with saying: there is a picture of the truth, haha!

Copy the generated file to the corresponding directory according to the help document. Next we will write a program in QtCreator to test whether our driver is available:

The function of the program is to output the database driver contained in Qt in the current system. The source code is as follows:

 
 
   
#include <QApplication>#include <QSqlDatabase>#include <QStringList>#include <QDebug>int main(int argc, char* argv[]) {    QApplication app(argc, argv);    qDebug() << "Available drivers:";    QStringList drivers = QSqlDatabase::drivers();    foreach(QString driver, drivers)        qDebug() << "\t" << driver;    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");    qDebug() << "ODBC driver valid?" << db.isValid();}
The program output result is as follows:

From the above output, we can see that the SQLite built-in with Qt and the MySQL database driver compiled by ourselves can work normally, and we do not support the ODBC database driver without compilation!

 

Note:

Why did it take me nearly a day for such a simple thing? Here we will talk about our own ideas and practices to avoid detours and familiarize you with how to compile the Qt module.

At first, I directly compiled the Qt source code without using the installed Qt tool to modularize the database driver. There are two problems:

1. Compile directly. It takes a long time to complete the compilation. If you use the configure Configuration tool to drive some modules, an error will be reported during compilation;

2. At this time, the path between specifying the MySQL header file and the library file is quite tangled. Maybe I am not familiar with Windows, and I feel tangled.

Later, I prepared modular compilation. This is really simple and will be finished soon. Pay special attention to the above steps to use the MinGW tool to convert MySQL database files.

 

Now, let's get there!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.