Connect MySQL using Qt in the Windows environment

  • 2020-05-26 09:44:40
  • OfStack

If the application only needs to connect to the remote database, then there is no need to install MySQL locally, just find the libmysql.dll and libmysqld.dll dynamic connection libraries provided by MySQL and add them to the Qt installation directory \5.9\mingw53_32\bin\; Qt by default comes with qsqlmysql.dll and qsqlmysqld.dll compiled (file path is Qt installation directory \5.9\mingw53_32\plugins\sqldrivers); If the two are matched, Qt can be successfully connected to MySQL.

(the version of Qt I installed is Qt 5.9.0 mingw53_32. Some of the paths mentioned in this article are the paths on my own machine, which need to be modified appropriately.)

1. Test whether Qt and MySQL can connect normally

Assuming that Qt installation directory \5.9\mingw53_32\bin\ has been added, libmysql.dll and libmysqld.dll are tested as follows.

The & # 8226; New Qt Widgets Application, modify main. cpp code is:


 #include "mainwindow.h"
 #include <QApplication>
 #include <QtSql>
 #include <QDebug>

 int main(int argc, char *argv[])
 {
   QApplication a(argc, argv);
   MainWindow w;
   w.show();

   // Establish a connection 
   QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
   db.setHostName("localhost");
   db.setPort(3306);
   db.setDatabaseName("mysql");
   db.setUserName("root");
   db.setPassword("yourPassword");   // Set the password for the database connection account 
   bool ok = db.open();
   if(ok) qDebug()<<"OK";
   else qDebug()<<"False";

   return a.exec();
 }

The & # 8226; Build and run the project and view the application output
If OK is output, that's it. Qt and MySQL are connected normally. If False is output, it is not surprising that libmysql.dll, libmysqld.dll and qsqlmysql.dll, qsqlmysqld.dll versions do not match! qsqlmysql.dll and qsqlmysqld.dll need to be modified.

2. Add libmysql.dll, libmysqld.dll for MySQL

(note: Qt msvc2015_64 can be used with 32
Bit, 64-bit version MySQL file, Qt mingw53_32 can only use 32-bit version MySQL file)
As mentioned earlier, if your application only needs to connect to a remote database, then you don't need to install MySQL locally, just use the dynamic connection libraries libmysql.dll and libmysqld.dll provided by MySQL. So if you don't install MySQL, how do you get those two dynamic link library files? You can do this:
The & # 8226; Copy these two files from the machine on which MySQL is installed.
The & # 8226; Temporarily install MySQL locally, then save the required files and uninstall MySQL.

To install MySQL, you can download the installation package from the official website. However, I don't like to do this, because there are too many installation components for MySQL, and many things are unnecessary. It is recommended to download the corresponding versions on some open source mirror sites, such as Tuna and USTC. After temporarily installing MySQL, don't rush to uninstall it. The MySQL driver will be used later when compiling MySQL.

3. Recompile qsqlmysql.dll, qsqlmysqld.dll

To compile Qt's MySQL driver, you need to use Qt's source code. Qt's source code can be downloaded Src using MaintenanceTool. exe. The source code is about the size of 2G, and the engineering files needed to compile the driver are about 10M. If the storage space and network speed are not enough, it is recommended to download only
qtbase-opensource-src-5.9.0. zip, required works are at qtbase-opensource-src-5.9.0 \src\plugins\sqldrivers\mysql\.

Open the project file for the compiler driver mentioned above with Qt and add these two sentences at the end of mysql.pro:
INCLUDEPATH += mysql installation directory \include
LIBS += -Lmysql installation directory \lib\ -llibmysql

Building and running the project will generate the plugins\sqldrivers directory under the C disk, which contains the qsqlmysql.dll and qsqlmysqld.dll files. Just copy them and overwrite the original two files. Did the connection test between Qt and MySQL succeed?


Related articles: