QT mysql driver

(本文转自网络)

Create MySQL driver for Qt5 on Windows

Posted on 19 February 2013 - 14:24

This tutorial explains how to create the MySQL driver for Qt5 on Windows using MinGW. If you are looking for the tutorial to do this with Qt4, click here.

Step 1

Download all the needed software.

  • Qt (libraries and source code)
    This are two separate files! E.g. qt-windows-opensource-5.0.1-mingw47_32-x86-offline.exe and qt-everywhere-opensource-src-5.0.1.zip
  • MySQL Community Server (E.g. mysql-5.5.30-win32.msi)
    I didn't use version 5.6 because the .msi only allowed me to install the 64-bit version of the Client C API Library, which apparently gives errors later in the tutorial. So make sure you get the 32-bit verison! If you really want version 5.6, an option might be to download the 32-bit version of the source files and use those. Note that I didn't test this option, so I can't help you with that.
Step 2

Install the downloaded software.

  • Install Qt, the components I installed are the default ones. (See image 1)
  • Install MySQL, you only need to install the Client C API Library the other components are optional for this tutorial. (See image 2)

    I installed it under C:\Program Files (x86)\MySQL\MySQL Server 5.5. Since the path contains spaces, you should useC:\PROGRA~2\MySQL\MYSQLS~1.5, else it won't work. 1

  • Extract the Qt Sources to a folder of your choosing. I placed it in the folder Qt was installed into. (C:\Qt\Qt5.0.1)
    I also renamed the extracted folder to Sources instead of the long qt-everywhere-opensource-src-5.0.1 name.

QT mysql driver_第1张图片Image 1: Install Qt

QT mysql driver_第2张图片Image 2: Install MySQL

Step 3

Open the Qt Command Prompt. (See image 3)
Start > All Programs > Qt 5.0.1 > 5.0.1 > MinGW 4.7 > Qt 5.0.1 for Desktop (MinGW 4.7)

Picasa Content
Image 3: Qt
Command Prompt

Step 4

Run the following commands (change them according to your environment):

  1. set mysql=C:\\PROGRA~2\\MySQL\\MYSQLS~1.5

  2. cd C:\Qt\Qt5.0.1\Sources\qtbase\src\plugins\sqldrivers\mysql\

  3. qmake "INCLUDEPATH+=%mysql%\\include" "LIBS+=%mysql%\\lib\\libmysql.lib" -o Makefile mysql.pro

  4. mingw32-make

Step 5
Picasa Content

Copy qsqlmysql.dll and qsqlmysqld.dll from C:\Qt\Qt5.0.1\Sources\qtbase\plugins\sqldrivers toC:\Qt\Qt5.0.1\5.0.1\mingw47_32\plugins\sqldrivers.

Step 6

Copy libmysql.dll from %mysql%\lib to C:\Windows

Finished

That's all, but don't forget to add QT += sql to your project file, else it won't work. To check which drivers are available run this program. You can also download the code as a Qt project in the attachments.

  1. #include <QtCore/QCoreApplication>

  2. #include <QtSQL>

  3. int main(int argc, char *argv[])

  4. {

  5. QCoreApplication a(argc, argv);

  6. qDebug() << QSqlDatabase::drivers();

  7. return a.exec();

  8. }

1 Note: For 32-bit systems the install path of MySQL will be C:\Program Files\MySQL\MySQL Server 5.5 and the path without spaces will beC:\PROGRA~1\MySQL\MYSQLS~1.5.To determine a custom path without spaces, use the dir /x command on each of the folders.

Attachments:

package-x-generic MysqlDriver.zip

你可能感兴趣的:(QT mysql driver)