0

I know there a many questions and answers on SO for this question, but nothing worked for me.

I installed the newest QT Creator 4.9.2 with Qt 5.13.0 MinGW 32-Bit and MySQLServer 5.5 to compile the QMYSQL driver. I but the driver in the sqldrivers folder and the libmysql.dll in the bin folder.

If I use C++ to connect to the database, everything is working fine, but I doesn't with python.

Additionally I installed on a fresh Python 3.7 32-Bit:

  • PyQt5: pip install pyqt5 (5.13.0)
  • mysqlclient: pip install mysqlclient (1.4.2)
  • mysql-connector: pip install mysql-connector (2.2.9)

and but the driver I compiled before in the sqldrivers folder from PyQt5, because the driver was missing there as well.

My Class:

class SqlConnector(QSqlDatabase):
    def __init__(self):
        super(SqlConnector, self).__init__()
        self.db = self.connect()

    def __del__(self):
        self.db.close()

    def connect(self):
        db = QSqlDatabase.addDatabase("QMYSQL")
        db.setHostName("hostname")
        db.setDatabaseName("database")
        db.setUserName("username")
        db.setPassword("1234567890")

        ok = db.open()

        if ok:
            print("Connection established")
            return db
        else:
            print("Connection failed: ", db.lastError().text())
            return None

    def getPools(self):
        query = QSqlQuery("SELECT id, name, token, dlxCode FROM AnalysisPool", self.db)

        model = QSqlQueryModel()
        model.setQuery(query)

        return model

Main Method:

from PyQt5.QtSql import QSqlDatabase, QSqlQueryModel, QSqlQuery
from PyQt5.QtWidgets import QApplication, QTableView

if __name__ == "__main__":
    app = QApplication(sys.argv)

    poolView = QTableView()
    poolView.show()

    database = SqlConnector()

    # poolView.setModel(database.getPools())

    sys.exit(app.exec_())

The libraryPaths from PyQt are C:/Python/lib/site-packages/PyQt5/Qt/plugins and C:/Python

The used OS is Windows 10 (1809) 64-Bit and I included in the path variable the MySQLServer lib folder.

I don't know what to do, I have read every SO question about this topic without a working solution for my problem.

crysis909
  • 25
  • 9
  • what is the output of `print(QtCore.QCoreApplication.libraryPaths())`? – eyllanesc Jul 12 '19 at 15:18
  • @eyllanesc The output is `['C:/Python/lib/site-packages/PyQt5/Qt/plugins', 'C:/Python']` – crysis909 Jul 12 '19 at 15:30
  • try copying `libmysql.dll` to `C:/Python/lib/site-packages/PyQt5/Qt/plugins/sqldrivers` – eyllanesc Jul 12 '19 at 15:38
  • Nothing changed, I get the same error again. The driver was not loaded. – crysis909 Jul 12 '19 at 15:55
  • on Linux I had to copy `libqsqlmysql.so` (similar to `libmysql.dll` on Windows) to this folder - and `PyQt` could find it but it still couldn't load it. I found in other question that using `ldd libqsqlmysql.so` I can see want other libraries it needs to work and I found I need also `libmysqlclient.so.18` which is part of `MySQL`, not `PyQt` - I had only `libmysqlclient.so.20`. After installing correct version it works for me on Linux. – furas Nov 22 '19 at 12:50

1 Answers1

0

For me worked this way: UNinstall package insraled via pip3. pip3 uninstall PyQt5 Install the native package for your distro (linux case). In my case is Fedora then Sudo dnf install python3-qt5 python3-qt5-qtbase --allowerasing Use an option wich lets you overwrite old files like --force or --allowerasing or something equivalent