[Problem Record] Solve the problem of Qt connecting to MySQL reporting "QMYSQL driver not loaded" and not supporting MySQL transaction operations!

EnvironmentEnvironment

  • Windows 11 Home Chinese version, 64-bit operating system, x64-based processor
  • Qt 5.15.2 MinGW 32-bit
  • mysql  Ver 14.14 Distrib 5.7.42, for Win32 (AMD64)

  1. After writing the code to connect to the MySQL database in Qt 5.15.2, and using the MinGW 32-bit build kit to compile and run, a "QMYSQL driver not loaded" error was reported!
  2. At work, I also encountered the problem that transaction operations cannot be enabled when operating the MySql database in Qt 5.15.2! The statement to determine whether MySQL transaction operations are supported is as follows:
    1. m_db.driver()->hasFeature(QSqlDriver::DriverFeature::Transactions);

questionstepminuteanalysis

Question one

  1. The "QMYSQL driver not loaded" error reported by Qt when connecting to MySQL is obviously a problem with the MySQL driver.
  2. I also encountered the problem of driver not loading in Qt 5.9.6 before. After clicking this article [Problem Record] Qt connected to MySQL and reported QMYSQL driver not loaded error! When operating, it still cannot be solved because the Qt version used is different, so the driver requirements for MySQL will also change.

Question 2

  1. Qt 5.15.2 should support transaction operations for MySQL databases, but specific support depends on the database driver and database engine used.
  2. The following are some possible reasons why transaction operations cannot be performed on the MySQL database in Qt 5.15.2:
    1. Use a database engine that does not support transactions : Some database engines (such as MySQL's MyISAM) do not support transaction operations. If you create a table on these engines, transaction operations will not be possible no matter what version of Qt you use. Make sure you are using a database engine that supports transactions, such as InnoDB.
    2. Using incorrect database driver: Qt uses different database drivers to connect to various databases. You may encounter problems if you use an incorrect database driver or do not support transaction operations. Make sure you select an appropriate database driver such as QMYSQL.
    3. Qt compilation issues: In some cases, the Qt library may not be compiled or configured correctly to support transactional operations for the database. Make sure that the Qt 5.15.2 version you are using has been built correctly and includes the SQL module.

Summarize

  1. Based on the analysis of the above situation, it is certain that the solution to this type of problem must start with the MySQL driver. However, the latest driver provided by the MySQL official website has stopped updating in 2017 and does not support the current Qt 5.15.2 version.
  2. So you still have to generate the MySQL driver suitable for the current Qt version based on the "MySQL source code part of the Qt source code" and the installed MySQL.

solutionmethod

Prerequisites

  1. Remember to check the Qt source code when installing Qt.
  2. The installation bit number of MySQL must be consistent with the number of Qt build kits (for example, if Qt uses MinGW 32-bit, the MySQL installation bit number must also be 32-bit).

Compile Qt's MySQL source code to generate the MySQL driver

  1. Copy the libmysql.dll and libmysql.lib library files in the D:\Software\IT\MySQL\mysql-5.7.42-win32\lib directory to the bin directory of the MinGW 32-bit compilation kit.
  2. Find the path where the MySQL source code is located (me: D:\Software\IT\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers\mysql), then double-click the mysql.pro file in the mysql directory and select it in Qt Creator MinGW 32-bit compilation suite.
  3. Open the mysql.pro file and edit as follows:
  4. Click the small hammer in the lower left corner of the Qt Creator interface to build the current mysql source code.
  5. Then the following files will be generated in the D:\Software\IT\Qt\5.15.2\mingw81_32\plugins\sqldrivers directory.
  6. Finally, run the example to test the MySQL driver, and the problem is finally solved! ! !

Remaining problem

  1. I encountered the following two errors, but I did not solve them, but the library finally generated solved the problem of Qt operating the MySQL database. (I will discuss the following two errors later, they are harmless for now)
    1. Cannot read D:/Software/IT/Qt/5.15.2/Src/qtbase/src/plugins/sqldrivers/qtsqldrivers-config.pri: No such file or directory.
    2. You need to set an executable in the custom run configuration.

Guess you like

Origin blog.csdn.net/weixin_43729127/article/details/133953112