Home > Database > Mysql Tutorial > How to use Qt connection for MySQL in Windows environment?

How to use Qt connection for MySQL in Windows environment?

黄舟
Release: 2017-07-17 14:31:27
Original
1336 people have browsed it

This article mainly introduces the relevant information about using Qt to connect MySQL in Windows environment. Friends who need it can refer to it

If the application only needs to connect to the remote database, then it is not required locally. Installation For MySQL, you only need to find the two dynamic link libraries libmysql.dll and libmysqld.dll provided by MySQL, and add them to the Qt installation directory\5.9\mingw53_32\bin\; by default Qt comes with it already compiled qsqlmysql.dll and qsqlmysqld.dll (the file path is Qt installation directory\5.9\mingw53_32\plugins\sqldrivers\); if they are matched, Qt can successfully connect to MySQL.

(The Qt version I installed is Qt 5.9.0 mingw53_32. Some of the paths mentioned in the article are the paths on my local machine and need to be modified appropriately)

1. Test whether Qt and MySQL can connect normally

Assuming that libmysql.dll and libmysqld.dll have been added to the Qt installation directory\5.9\mingw53_32\bin\, conduct the following test.

•Create a new Qt Widgets Application and modify the code of main.cpp to:

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

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

   //建立连接
   QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
   db.setHostName("localhost");
   db.setPort(3306);
   db.setDatabaseName("mysql");
   db.setUserName("root");
   db.setPassword("yourPassword");   //设置数据库连接账号的密码
   bool ok = db.open();
   if(ok) qDebug()<<"OK";
   else qDebug()<<"False";

   return a.exec();
 }
Copy after login

•Build and run the project and view the application output
If the output is OK, that’s it. The connection between Qt and MySQL is normal; if False is output, it is not unexpected because the versions of libmysql.dll, libmysqld.dll and qsqlmysql.dll, qsqlmysqld.dll do not match! qsqlmysql.dll and qsqlmysqld.dll need to be modified.

2. Add MySQL’s libmysql.dll and libmysqld.dll

(Note: Qt msvc2015_64 can use 32
and 64-bit versions of MySQL files. Qt mingw53_32 can only use the 32-bit version of MySQL files)
As mentioned before, if the application only needs to connect to the remote database, then there is no need to install MySQL locally, but only the dynamic link libraries libmysql.dll and libmysqld provided by MySQL. .dll. So if you don't install MySQL, how to obtain the two dynamic link library files? You can do this:
•Copy these two files from the machine where MySQL is installed.
•Temporarily install MySQL locally, then keep the required files and uninstall MySQL.

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

3. Recompile qsqlmysql.dll, qsqlmysqld.dll

Compiling Qt The MySQL driver requires the source code of Qt. To obtain the source code of Qt, you can use MaintenanceTool. exe download Src, the project file path used to compile the driver is Qt installation directory\5.9\Src\qtbase\src\plugins\sqldrivers\mysql\. The source code is almost 2G in size, and the project files needed to compile the driver are about tens of MB. If the storage space and network speed are not sufficient, it is recommended to only download
qtbase-opensource-src-5.9.0.zip, which is needed The project is in qtbase-opensource-src-5.9.0\src\plugins\sqldrivers\mysql\.

Use Qt to open the project file used to compile the driver mentioned above, 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 drive, which contains two files: qsqlmysql.dll and qsqlmysqld.dll. Copy them to overwrite the original ones. Two files are enough. Test the connection between Qt and MySQL again. Was it successful?

The above is the detailed content of How to use Qt connection for MySQL in Windows environment?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template