Driver not loaded Driver not loaded

本文档展示了在使用PyQt5时遇到的数据库连接问题,由于缺少qsqlmysql.dll导致无法正常连接MySQL数据库。解决方案是先安装PyQt5的5.12.1版本,该版本包含所需的库文件,然后卸载并重新安装5.15.2版本。通过这种方法,成功解决了问题并能执行SQL查询。
摘要由CSDN通过智能技术生成

代码如下:

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值