未加载驱动程序 mySql

Driver not loaded mySql

我正在使用 Pycharm 和 Python3.9 以及 PyQt 5.15.2 和 QSqlDatabase 我按照 https://doc.qt.io/qt-5/sql-driver.html#qmysql How to Build the QMYSQL Plugin on Windows. I used the mingw81_64 compiler. The file qsqlmysql.dll has been generated and I have copied it to PyQt5/Qt/plugins/sqldrivers in my virtual environment. I also copied the file libmydql.dll to PyQt5/Qt5/bin in my virtual environment. If I execute database.drivers() I get the following ['QSQLITE', 'QMARIADB', 'QMYSQL', 'QMYSQL3', 'QODBC', 'QODBC3', 'QPSQL', 'QPSQL7']. I got the following error "Driver not loaded" I also tried downloading the file qsqlmysql.dll from https://github.com/weldermarcosxd/pyqt5-mysql/blob/master/Lib/site-packages/PyQt5/Qt/plugins/sqldrivers/qsqlmysql.dll but I got the same error. I'm using this database https://remotemysql.com/ 上的说明进行操作,但我也尝试使用本地 MySql 服务器和我得到了同样的错误。 SQLlite 工作正常。

class Login(QWidget):
def __init__(self):
    super().__init__()
    self.init_widgets()
    self.set_styles()
    self.connectSQLlite()

def connect(self):
    server = 'remotemysql.com'
    database_name = '****'
    user_name = '****'
    password = '****'

    self.database = QSqlDatabase.addDatabase("QMYSQL")
    self.database.setHostName(server)
    self.database.setDatabaseName(database_name)
    self.database.setUserName(user_name)
    self.database.setPassword(password)
    ok = self.database.open()
    if ok:
        print('Success')
    else:
        print(self.database.lastError().text())

app = QApplication(sys.argv)

您必须使用与编译 Qt 二进制文件相同的编译器来编译插件。