1. Problem description
Generally speaking, pyqt can connect to MySQL database in the following format:
self.DB = QSqlDatabase.addDatabase('QMYSQL') self.DB.setDatabaseName("school_club") # Enter the data table you want to access self.DB.setHostName('localhost') self.DB.setPort(3306) self.DB.setUserName('root') self.DB.setPassword('') # Enter the password for your own database
However, sometimes the connection fails, and debug can’t point out the error clearly. Errors can be found in the following ways.
Method 1: check the driver attached to pyqt
If ‘qmmysql’ and ‘qmysql3’ are found missing in the driver, it indicates that the error is driver missing. You can use this method to solve the problem. Because under normal conditions, the output should be as follows:
['QSQLITE', 'QMYSQL', 'QMYSQL3', 'QODBC', 'QODBC3', 'QPSQL', 'QPSQL7']
Method 2: print error information
This method can also get the specific part of the connection error.
2. How to solve the lack of qmmysql driver?
The author’s pyqt is installed in Anaconda environment, so the folder path to be found by the driver is special.
The general idea is to add two DLL files: qsqlmysql.dll and libmysql.dll in pyqt related folder. At the same time, these two files must be fully matched with the relevant pyqt version.
Step 1: load qsqlmysql.dll
First, open Anaconda prompt, enter the following command line, and adjust the python version to 5.12.1 (only this version comes with qsqlmysql.dll, other versions of this DLL are too difficult to find a matching one)
pip install PyQt5==5.12.1
After this step, qsqlmysql.dll has been successfully installed in the relevant folder.
Step 2: load libmysql.dll
This is relatively easy. First, find the libmysql.dll (this is the default path for MySQL installation, and the user-defined path will be found separately) in the path of C:// program files/MySQL/MySQL server 8.0/bin, and copy it.
Take my computer as an example, paste the file to the following path:
You can choose the appropriate path according to the relative path of the above path according to the installation location of anaconda.
Restart the programming tool and print again (QSqlDatabase. Drivers()). It is found that ‘qmmysql’ and ‘qmysql3’ have successfully appeared.
- Mysql Flashback Warning: C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe
- Using Mybatis to connect to the database error: Loading class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver···
- host ‘‘ is not allowed to connect to this mysql server Connect MYSQL Error
- MYSQL 5.7 Error Code: 1290. The MySQL server is running with the –secure-file-priv option so it..
- How to Fix MySQL ERROR 1130 (HY000): Host ‘XXXX’ is not allowed to connect to this MySQL server
- MYSQL Error 1045 (28000): access denied for user ‘root’ @’localhost ‘(using password: Yes)
- How to Solve MYSQL Error: Failed to start MySQL 8.0 database server
- Problems in connecting mysql8.0 in IDEA [How to Solve]
- MySQL Install Error: MySQL error 1042: Unable to connect to any of the specified MySQL hosts
- [Solved] ERROR 2002 (HY000): Can’t connect to local MySQL server through socket’/var/lib/mysql/mysql.sock’ (2)
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’
- [Solved] Connections could not be acquired from the underlying database
- How to Solve can‘t connect to local mysql server through socket ‘/var/lib/mysql/mysql.sock‘
- [Solved] Error while trying to retrieve text for error ORA-01019
- Remote connection to MySQL database error: is not allowed to connect to this MYSQL server solution
- [Solved] Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- Mysql22.214.171.124 Error 1054 (42S22): Unknown Column’password’ In’field List’ When Changing User Password
- Ubuntu ODBC MySQL 8 OPTION NOT Work [How to Solve]
- ERROR 1054(42S22) Unknown column ‘password’ in ‘field list’ (Fixed)