Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Mac Pyside6 QSqlDatabase: QMYSQL driver not loaded
Qt 6.11 is out! See what's new in the release blog

Mac Pyside6 QSqlDatabase: QMYSQL driver not loaded

Scheduled Pinned Locked Moved Solved Qt for Python
15 Posts 5 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jerry0305

    Connect to the mysql example with Pyside6 QSqlDatabase

    import sys
    from PyQt6.QtCore import QSize, Qt
    from PyQt6.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
    from PyQt6.QtWidgets import QApplication, QMainWindow, QTableView
    db = QSqlDatabase('QMYSQL')
    db.setPort(3306)
    db.setHostName('localhost')
    db.setDatabaseName('test')
    db.setUserName('root')
    db.setPassword('123456')
    db.open()
    class MainWindow(QMainWindow):
    def init(self):
    super().init()

        self.table = QTableView()
    
        self.model = QSqlQueryModel()
        self.table.setModel(self.model)
    
        query = QSqlQuery("SELECT user_name FROM users", db=db)
    
        self.model.setQuery(query)
    
        self.setMinimumSize(QSize(1024, 600))
        self.setCentralWidget(self.table)
    

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

    The error message is as follows:
    QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE QODBC QPSQL
    QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
    QSqlQuery::exec: database not open

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @jerry0305 said in Mac Pyside6 QSqlDatabase: QMYSQL driver not loaded:

    QSqlDatabase: available drivers: QSQLITE QODBC QPSQL

    Implies you have not installed the QMYSQL driver.

    On a separate matter:

    QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins

    You cannot write your code as you have done with a global db = QSqlDatabase('QMYSQL') outside of anything. As the message tells you, you cannot execute such a statement till after app = QApplication(sys.argv). I suggest you look at some of the examples and follow them.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jerry0305
      wrote on last edited by
      #3

      After compiling the mysql driver through the source code, the problem has been solved, thank you for your suggestion.

      S 1 Reply Last reply
      0
      • J jerry0305

        After compiling the mysql driver through the source code, the problem has been solved, thank you for your suggestion.

        S Offline
        S Offline
        s907461903
        wrote on last edited by
        #4

        @jerry0305 How do you compile the mysql driver for pyside6? Could you please share your compliation progress with me when you are convenient?

        JonBJ 1 Reply Last reply
        0
        • S s907461903

          @jerry0305 How do you compile the mysql driver for pyside6? Could you please share your compliation progress with me when you are convenient?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #5

          @s907461903
          The driver must be compiled with C++ before it can be used from Python.

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @s907461903
            The driver must be compiled with C++ before it can be used from Python.

            S Offline
            S Offline
            s907461903
            wrote on last edited by
            #6

            @JonB I have compiled mysql's drivers (debug and release versions) based on the source code of qt-6.4.2, and the version mysql is " Ver 8.0.31 for Win64 on x86_64 (MySQL Community Server - GPL)". A problem named "Driver not loaded Driver not loaded" emerged in the following snip code.

            import sys
            from PySide6 import QtSql
            from PySide6.QtSql import QSqlQuery
            from PySide6.QtWidgets import QApplication, QTableView, QVBoxLayout, QWidget
            
            
            class MainWidget(QWidget):
                def __init__(self):
                    super(MainWidget, self).__init__()
                    self.db = None
                    self.tableView = QTableView()
                    layout = QVBoxLayout()
                    layout.addWidget(self.tableView)
                    self.setLayout(layout)
                    self.resize(600, 400)
                    print(f"available drivers: {QtSql.QSqlDatabase.drivers()}")
                    print(f"available drivers: {QtSql.QSqlDatabase.isDriverAvailable('QMYSQL')}")
                    self.readDb()
            
                def readDb(self):
                    # self.db = QtSql.QSqlDatabase.addDatabase('QMYSQL')
                    self.db = QtSql.QSqlDatabase('QMYSQL')
                    if self.db.isValid():
                        print("QSqlDatabase is valid.")
                    else:
                        print(f"QSqlDatabase is invalid. Error: {self.db.lastError().text()}")
                    self.db.setHostName('localhost')
                    self.db.setDatabaseName('db_test')
                    flag = self.db.open('root', '123456')
                    if flag:
                        print("Database initialized successfully.")
                        query = QSqlQuery(db=self.db)
                    else:
                        print("Database open failed.")
            
            
            if __name__ == "__main__":
                app = QApplication(sys.argv)
                win = MainWidget()
                win.show()
                sys.exit(app.exec())
            
            JonBJ 1 Reply Last reply
            0
            • S s907461903

              @JonB I have compiled mysql's drivers (debug and release versions) based on the source code of qt-6.4.2, and the version mysql is " Ver 8.0.31 for Win64 on x86_64 (MySQL Community Server - GPL)". A problem named "Driver not loaded Driver not loaded" emerged in the following snip code.

              import sys
              from PySide6 import QtSql
              from PySide6.QtSql import QSqlQuery
              from PySide6.QtWidgets import QApplication, QTableView, QVBoxLayout, QWidget
              
              
              class MainWidget(QWidget):
                  def __init__(self):
                      super(MainWidget, self).__init__()
                      self.db = None
                      self.tableView = QTableView()
                      layout = QVBoxLayout()
                      layout.addWidget(self.tableView)
                      self.setLayout(layout)
                      self.resize(600, 400)
                      print(f"available drivers: {QtSql.QSqlDatabase.drivers()}")
                      print(f"available drivers: {QtSql.QSqlDatabase.isDriverAvailable('QMYSQL')}")
                      self.readDb()
              
                  def readDb(self):
                      # self.db = QtSql.QSqlDatabase.addDatabase('QMYSQL')
                      self.db = QtSql.QSqlDatabase('QMYSQL')
                      if self.db.isValid():
                          print("QSqlDatabase is valid.")
                      else:
                          print(f"QSqlDatabase is invalid. Error: {self.db.lastError().text()}")
                      self.db.setHostName('localhost')
                      self.db.setDatabaseName('db_test')
                      flag = self.db.open('root', '123456')
                      if flag:
                          print("Database initialized successfully.")
                          query = QSqlQuery(db=self.db)
                      else:
                          print("Database open failed.")
              
              
              if __name__ == "__main__":
                  app = QApplication(sys.argv)
                  win = MainWidget()
                  win.show()
                  sys.exit(app.exec())
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #7

              @s907461903
              In a terminal/shell set an environment variable QT_DEBUG_PLUGINS=1 and then run your Python script/application. You should get diagnostic output for why it can't find the QMYSQL driver.

              S 1 Reply Last reply
              0
              • JonBJ JonB

                @s907461903
                In a terminal/shell set an environment variable QT_DEBUG_PLUGINS=1 and then run your Python script/application. You should get diagnostic output for why it can't find the QMYSQL driver.

                S Offline
                S Offline
                s907461903
                wrote on last edited by
                #8

                @JonB Our os is window10 64-bit and the python interpreter is python 3.8 installed in Anaconda3-2020. According to your advise, we test the snip code and some issues emeres, as sniped below:
                11.png

                JonBJ 1 Reply Last reply
                0
                • S s907461903

                  @JonB Our os is window10 64-bit and the python interpreter is python 3.8 installed in Anaconda3-2020. According to your advise, we test the snip code and some issues emeres, as sniped below:
                  11.png

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @s907461903
                  It's a lot easier for people is you copy & paste text than show a screen shot. Anyway I can see there it says what the problem is for the QMYSQL driver.

                  S 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @s907461903
                    It's a lot easier for people is you copy & paste text than show a screen shot. Anyway I can see there it says what the problem is for the QMYSQL driver.

                    S Offline
                    S Offline
                    s907461903
                    wrote on last edited by
                    #10

                    @JonB Hello, JonB, I am sorry to that. The two drivers (qsqlmysql.dll and qsqlmysqld.dll) have been placed in the correct directory. And the detailed problem is shown below:

                    qt.core.plugin.factoryloader: Got keys from plugin meta data QList("QPSQL")
                    qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/sqldrivers" ...
                    available drivers: ['QSQLITE', 'QMARIADB', 'QMYSQL', 'QODBC', 'QPSQL']
                    available drivers: True
                    qt.core.library: "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/sqldrivers/qsqlmysql.dll" cannot load: Cannot load library C:\Anaconda3\envs\packageEnv\lib\site-packages\PySide6\plugins\sqldrivers\qsqlmysql.dll: 找不到指定的模块。
                    qt.core.plugin.loader: QLibraryPrivate::loadPlugin failed on "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/sqldrivers/qsqlmysql.dll" : "Cannot load library C:\\Anaconda3\\envs\\packageEnv\\lib\\site-packages\\PySide6\\plugins\\sqldrivers\\qsqlmysql.dll: 找不到指定的模块。"
                    QSqlDatabase: QMYSQL driver not loaded
                    QSqlDatabase: available drivers: QSQLITE QMARIADB QMYSQL QODBC QPSQL
                    QSqlDatabase is invalid. Error: Driver not loaded Driver not loaded
                    Database open failed.
                    qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/accessible" ...
                    qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/accessible" ...
                    qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/accessiblebridge" ...
                    qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/accessiblebridge" ...
                    
                    jsulmJ JonBJ 2 Replies Last reply
                    0
                    • S s907461903

                      @JonB Hello, JonB, I am sorry to that. The two drivers (qsqlmysql.dll and qsqlmysqld.dll) have been placed in the correct directory. And the detailed problem is shown below:

                      qt.core.plugin.factoryloader: Got keys from plugin meta data QList("QPSQL")
                      qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/sqldrivers" ...
                      available drivers: ['QSQLITE', 'QMARIADB', 'QMYSQL', 'QODBC', 'QPSQL']
                      available drivers: True
                      qt.core.library: "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/sqldrivers/qsqlmysql.dll" cannot load: Cannot load library C:\Anaconda3\envs\packageEnv\lib\site-packages\PySide6\plugins\sqldrivers\qsqlmysql.dll: 找不到指定的模块。
                      qt.core.plugin.loader: QLibraryPrivate::loadPlugin failed on "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/sqldrivers/qsqlmysql.dll" : "Cannot load library C:\\Anaconda3\\envs\\packageEnv\\lib\\site-packages\\PySide6\\plugins\\sqldrivers\\qsqlmysql.dll: 找不到指定的模块。"
                      QSqlDatabase: QMYSQL driver not loaded
                      QSqlDatabase: available drivers: QSQLITE QMARIADB QMYSQL QODBC QPSQL
                      QSqlDatabase is invalid. Error: Driver not loaded Driver not loaded
                      Database open failed.
                      qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/accessible" ...
                      qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/accessible" ...
                      qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/accessiblebridge" ...
                      qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/accessiblebridge" ...
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @s907461903 said in Mac Pyside6 QSqlDatabase: QMYSQL driver not loaded:

                      找不到指定的模块。

                      Would be good if you would translate it.
                      According to Goodle Translate it means that C:\Anaconda3\envs\packageEnv\lib\site-packages\PySide6\plugins\sqldrivers\qsqlmysql.dll was not found. So, does this file exist?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      S 1 Reply Last reply
                      0
                      • S s907461903

                        @JonB Hello, JonB, I am sorry to that. The two drivers (qsqlmysql.dll and qsqlmysqld.dll) have been placed in the correct directory. And the detailed problem is shown below:

                        qt.core.plugin.factoryloader: Got keys from plugin meta data QList("QPSQL")
                        qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/sqldrivers" ...
                        available drivers: ['QSQLITE', 'QMARIADB', 'QMYSQL', 'QODBC', 'QPSQL']
                        available drivers: True
                        qt.core.library: "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/sqldrivers/qsqlmysql.dll" cannot load: Cannot load library C:\Anaconda3\envs\packageEnv\lib\site-packages\PySide6\plugins\sqldrivers\qsqlmysql.dll: 找不到指定的模块。
                        qt.core.plugin.loader: QLibraryPrivate::loadPlugin failed on "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/sqldrivers/qsqlmysql.dll" : "Cannot load library C:\\Anaconda3\\envs\\packageEnv\\lib\\site-packages\\PySide6\\plugins\\sqldrivers\\qsqlmysql.dll: 找不到指定的模块。"
                        QSqlDatabase: QMYSQL driver not loaded
                        QSqlDatabase: available drivers: QSQLITE QMARIADB QMYSQL QODBC QPSQL
                        QSqlDatabase is invalid. Error: Driver not loaded Driver not loaded
                        Database open failed.
                        qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/accessible" ...
                        qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/accessible" ...
                        qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/lib/site-packages/PySide6/plugins/accessiblebridge" ...
                        qt.core.plugin.factoryloader: checking directory path "C:/Anaconda3/envs/packageEnv/accessiblebridge" ...
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #12

                        @s907461903
                        Either C:\Anaconda3\...\qsqlmysql.dll file does not exist or it requires dependent libraries and they do not exist/cannot be found but it still gives the message on the DLL file.

                        S 1 Reply Last reply
                        0
                        • SGaistS SGaist moved this topic from General and Desktop on
                        • jsulmJ jsulm

                          @s907461903 said in Mac Pyside6 QSqlDatabase: QMYSQL driver not loaded:

                          找不到指定的模块。

                          Would be good if you would translate it.
                          According to Goodle Translate it means that C:\Anaconda3\envs\packageEnv\lib\site-packages\PySide6\plugins\sqldrivers\qsqlmysql.dll was not found. So, does this file exist?

                          S Offline
                          S Offline
                          s907461903
                          wrote on last edited by
                          #13

                          @jsulm Thanks for you comments. I have placed the two dll files (qsqlmysql.dll and qsqlmysqld.dll) in "C:\Anaconda3\envs\packageEnv\Lib\site-packages\PySide6\plugins\sqldrivers". And a dll file (libmysql.dll) copied from "C:\Program Files\MySQL\MySQL Server 8.0\lib\libmysql.dll" is also put in the same directory. I do not understand why the QtSql can't load or find these dll files.

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @s907461903
                            Either C:\Anaconda3\...\qsqlmysql.dll file does not exist or it requires dependent libraries and they do not exist/cannot be found but it still gives the message on the DLL file.

                            S Offline
                            S Offline
                            s907461903
                            wrote on last edited by
                            #14

                            @JonB I check many times that the two dll files (qsqlmysql.dll and qsqlmysqld.dll) do exist in the path. I can connect to MySQL Database in programm based on PyQt5 in the same configurations. In PySide6 programm, I use the source code of Qt6.4.2 to compile the abovementioned dll files and place them in corresponding directory.

                            J 1 Reply Last reply
                            0
                            • S s907461903

                              @JonB I check many times that the two dll files (qsqlmysql.dll and qsqlmysqld.dll) do exist in the path. I can connect to MySQL Database in programm based on PyQt5 in the same configurations. In PySide6 programm, I use the source code of Qt6.4.2 to compile the abovementioned dll files and place them in corresponding directory.

                              J Offline
                              J Offline
                              Johnnyhq
                              wrote on last edited by
                              #15

                              @s907461903 I think your Qt version and PySide6 version are not the same. Please check that the version of the dll files (qsqlmysql.dll and qsqlmysqld.dll) is the same as the source code you compile from Qt6.4.2.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved