代码如下:
import redis
from PyQt5.Qt import *
from resource.EquToMesEquipmentProcessParam_window import Ui_Form
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtSql import QSqlDatabase, QSqlQueryModel
from PyQt5.QtWidgets import QApplication, QMessageBox, QTableView
# class EquToMesEquipmentProcessParamPane(QWidget, QTableView, Ui_Form):
class EquToMesEquipmentProcessParamPane(QTableView):
def __init__(self):
super(EquToMesEquipmentProcessParamPane, self).__init__()
# self.db = None
self.db_connect()
self.sql_exec()
def db_connect(self):
self.db = QSqlDatabase.addDatabase('QMYSQL')
self.db.setHostName('localhost')
self.db.setPort(3306)
self.db.setDatabaseName('suining_svolt_xadmin_v1')
self.db.setUserName('root')
self.db.setPassword('root')
if not self.db.open():
QMessageBox.critical(self, 'Database Connection', self.db.lastError().text())
def closeEvent(self, QCloseEvent):
self.db.close()
def sql_exec(self):
model = QSqlQueryModel() # 1
model.setQuery("SELECT id, name, class, score FROM equtomesequipmentprocessparam_equtomesequipmentprocessparampo")
model.setHeaderData(0, Qt.Horizontal, 'ID')
model.setHeaderData(1, Qt.Horizontal, 'Name')
model.setHeaderData(2, Qt.Horizontal, 'Class')
model.setHeaderData(3, Qt.Horizontal, ' Score')
self.setModel(model) # 2
for i in range(model.rowCount()): # 3
id = model.record(i).value('id')
name = model.record(i).value(1)
print(id, name)
print('---------------------')
for i in range(model.rowCount()): # 4
id = model.data(model.index(i, 0))
name = model.data(model.index(i, 1))
print(id, name)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = EquToMesEquipmentProcessParamPane()
window.show()
sys.exit(app.exec_())
解决办法:
目前 PyQt5 是5.15.2
需要安装 5.12.1 ,这个版本有 qsqlmysql.dll 。拷贝下来
再安装 5.15.2
C:\Users\huanglianggu\PycharmProjects\suining-svolt\suining_svolt_xadmin_v1>pip install PyQt5==5.12.1 -i https://pypi.tuna.tsinghua.edu.cn/simple/
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting PyQt5==5.12.1
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/b2/87/219cd547c0deb902edd3fdbf1af01409f7f9d251344b9154d878203cf841/PyQt5-5.12.1-5.12.2-cp35.cp36.cp37.cp38-none-win_amd64.whl
Requirement already satisfied: PyQt5_sip<4.20,>=4.19.14 in c:\users\huanglianggu\appdata\local\programs\python\python38\lib\site-packages (from PyQt5==5.12.1) (4.19.19)
Installing collected packages: PyQt5
Found existing installation: PyQt5 5.15.2
Uninstalling PyQt5-5.15.2:
Successfully uninstalled PyQt5-5.15.2
Successfully installed PyQt5-5.12.1
WARNING: You are using pip version 19.2.3, however version 22.0.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
C:\Users\huanglianggu\PycharmProjects\suining-svolt\suining_svolt_xadmin_v1>pip install PyQt5==5.15.2 -i https://pypi.tuna.tsinghua.edu.cn/simple/
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting PyQt5==5.15.2
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/aa/72/754c693db0e745b9fe47debc3ec52844461f090d5beff28489a0cde5ef82/PyQt5-5.15.2-5.15.2-cp35.cp36.cp37.cp38.cp39-none-win_amd64.whl
Collecting PyQt5-sip<13,>=12.8 (from PyQt5==5.15.2)
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/39/d0/cf2b613a3309397013e7781f2cdb4b1e0661ae635d88b75b6f481ec43d8c/PyQt5_sip-12.10.1-cp38-cp38-win_amd64.whl (77kB)
|████████████████████████████████| 81kB 102kB/s
Installing collected packages: PyQt5-sip, PyQt5
Found existing installation: PyQt5-sip 4.19.19
Uninstalling PyQt5-sip-4.19.19:
Successfully uninstalled PyQt5-sip-4.19.19
Found existing installation: PyQt5 5.12.1
Uninstalling PyQt5-5.12.1:
Successfully uninstalled PyQt5-5.12.1
Successfully installed PyQt5-5.15.2 PyQt5-sip-12.10.1
WARNING: You are using pip version 19.2.3, however version 22.0.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
C:\Users\huanglianggu\PycharmProjects\suining-svolt\suining_svolt_xadmin_v1>