Reference article:
80261695
The previous article said how to install MySQL driver for PyQt5 in Ubuntu environment, which is mainly about how to install MySQL driver in Windows environment.
#-*-coding:utf-8-*-" ""Introduction" Processing database example in PyQt5" "ImportSYS fromPyqt5.qtcoreImport* fromPyqt5.qtguiImport* fromPyqt5.qtwidgetsImport* fromPyqt5.qtsqlImportQsqldatabaseclassExecdatabasedemo (qwidget):def __init__(Self, parent=None): Super (Execdatabasedemo, self).__init__(parent)#self.db = qsqldatabase.adddatabase (' qsqlite ') #self.db.setDatabaseName ('./db/database.db ') #Open Database #Self.db.open ()Self.con2= Qsqldatabase.adddatabase ('Qmysql') Self.con2.setHostName ("67.209.xxx.xxx") Self.con2.setDatabaseName ("MySQL") Self.con2.setUserName ("Root") Self.con2.setPassword ("xxxxxxxx") A=Self.con2.open ()Print(a)defcloseevent (Self, event):#Close the databaseself.con2.close ()if __name__=='__main__': App=qapplication (SYS.ARGV) Demo=Execdatabasedemo () demo.show () Sys.exit (App.exec_ ())
The above is the specific code, after running always print False, which indicates that the database is not connected.
In a Windows environment, the code runs in my programming environment without prompting for any errors, which is different from the Linux environment.
After finding the information on the Internet find the file LibmySQL.dll, this dynamic library can be done, the general installation of MySQL can be found on the computer, the default is generally C:\Program files\mysql\mysql Server 5.6\lib Copy the libmysql.dll file inside
If the Python version is 32-bit, then you have to find the 32-bit Libmysql file.
If the Python version is 64-bit, then you have to find the 64-bit Libmysql file.
One final step:
Put the copy of the libmysql.dll file in the bin directory of the PYQT5/QT, for example, mine is:
C:\Users\devil\AppData\Local\Programs\Python\Python35\Lib\site-packages\PyQt5\Qt\bin
Run the database connection code again, and find that you can connect correctly.
Windows environment PyQt5 How to install MySQL driver (PyQt5 display driver not loaded solution when connecting to MySQL)