程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python programming instance -pyqt5 GUI programming -mysql database driver plug-in installation and use of pyqt5

編輯:Python

PyQt5 Of MySQL Database driver plug-in installation and use

Qt5 Do not install by default MySQL Database driven , This gives support to different versions of MySQL Database support provides flexible support , At the same time, it improves MySQL The difficulty of using the database . This article details how to install MySQL Database driver plug-in and use .

1、 Software preparation

In the installation MySQL Before plug in , You need to prepare the following software :

  • MySQL database , The version used in this instance is 8.0.28, This version comes with MySQL Connector C, Therefore, no additional installation is required MySQL Connector C.

  • Visual Studio 2019 Community version ,Visual Studio 2019 Need to install VC++ Function module

  • Qt frame : Used in this instance Qt The Framework version is 5.15.2, And install Qt Framework source code

  • Python development environment :anaconda,Anaconda Of Python Version is 3.9.7

  • install PyQt5 And its related components :

    pip install PyQt5==5.15.2 pyqtwebengine==5.15.2 pyqtdatavisualization==5.15.2 pyqtchart==5.15.2 pyqt3d==5.15.2 pyqt5-plugins==5.15.2.2.2 qscintilla==2.12.0
    

2、 compile MySQL Database driver plug-in

First step : open Visual Studio 2019 The command line :X64 Native Tools Command Prompt for VS 2019

The second step : Navigate the directory to... In the command Qt Install under directory sql Driver plug-in Directory :

cd %QTDIR%\Src\qtbase\src\plugins\sqldrivers

The location of this article is :

C:\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers

The third step : To configure MySQL Database driven plug-in compilation options :

install MySQL Connector C edition

qmake -- MYSQL_INCDIR="C:/Program Files/MySQL/MySQL Connector C 6.1/include" MYSQL_LIBDIR="C:/Program Files/MySQL/MySQL Connector C 6.1/lib"

The command line used this time is as follows :

qmake -- MYSQL_INCDIR="C:\Program Files\MySQL\MySQL Server 8.0\include" MYSQL_LIBDIR="C:\Program Files\MySQL\MySQL Server 8.0\lib"

Be careful ,qmake The command must add the current command line environment

Step four : Compile and install MySQL Database driver plug-in

nmake sub-mysql
nmake install

After compiling and installing ,MySQL The database plug-in is automatically installed to ,Qt Installation position of frame . The location here is :

C:\Qt\5.15.2\msvc2019_64\plugins\sqldrivers

Finally, put the... In the red box 4 Files copied to PyQt5 In the database plug-in folder of , The position here is :

C:\Anaconda3\Lib\site-packages\PyQt5\Qt\plugins\sqldrivers

Please follow your own Anaconda Install find .

Step five , take MySQL Drive support DLL Copied to the Anaconda The root directory of the installation directory :

Be careful , If you do not copy these files to Anaconda Root directory or other system environment , Otherwise, the subsequent database connection will not succeed .

3、 Connect MySQL

Complete the previous steps , It can be used normally MySQL Database driven plug-ins .

First step , Import dependency Library :

import sys
from PyQt5.QtSql import QSqlDatabase

The second step , Query the current database driver plug-in

drivers = QSqlDatabase.drivers()
print(drivers)

The output here is as follows :

[‘QSQLITE’, ‘QMARIADB’, ‘QMYSQL’, ‘QMYSQL3’, ‘QODBC’, ‘QODBC3’, ‘QPSQL’, ‘QPSQL7’]

You can tell from the output ,QMYSQL Express MySQL The database driver has been successfully installed

The third step , Connect to database

# load MySQL Database driven 
db = QSqlDatabase.addDatabase("QMYSQL")
# Set up the database IP Address 
db.setHostName('localhost')
# Set database port 
db.setPort(3306)
# Please modify according to your own database name 
db.setDatabaseName(' Database name ')
db.setUserName('MySQL Database user name ')
db.setPassword('MySQL Database user login password ')
# Connect and open the database 
db.open()
if db.isOpen():
print("connected to mysql database")
else:
print("mysql connect failed:",db.lastError().text())
sys.exit()
# Close the database 
db.close()

The output here is as follows :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved