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
Forum Updated to NodeBB v4.3 + New Features

Mac Pyside6 QSqlDatabase: QMYSQL driver not loaded

Scheduled Pinned Locked Moved Solved Qt for Python
15 Posts 5 Posters 1.9k 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 Offline
    J Offline
    jerry0305
    wrote on 15 Aug 2022, 06:21 last edited by
    #1

    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

    J 1 Reply Last reply 15 Aug 2022, 08:02
    0
    • J jerry0305
      15 Aug 2022, 06:21

      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

      J Offline
      J Offline
      JonB
      wrote on 15 Aug 2022, 08:02 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 16 Aug 2022, 05:25 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 8 Feb 2023, 09:31
        0
        • J jerry0305
          16 Aug 2022, 05:25

          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 8 Feb 2023, 09:31 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?

          J 1 Reply Last reply 8 Feb 2023, 09:36
          0
          • S s907461903
            8 Feb 2023, 09:31

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

            J Offline
            J Offline
            JonB
            wrote on 8 Feb 2023, 09:36 last edited by
            #5

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

            S 1 Reply Last reply 9 Feb 2023, 09:34
            0
            • J JonB
              8 Feb 2023, 09:36

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

              S Offline
              S Offline
              s907461903
              wrote on 9 Feb 2023, 09:34 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())
              
              J 1 Reply Last reply 9 Feb 2023, 09:38
              0
              • S s907461903
                9 Feb 2023, 09:34

                @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())
                
                J Offline
                J Offline
                JonB
                wrote on 9 Feb 2023, 09:38 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 9 Feb 2023, 09:56
                0
                • J JonB
                  9 Feb 2023, 09:38

                  @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 9 Feb 2023, 09:56 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

                  J 1 Reply Last reply 9 Feb 2023, 09:59
                  0
                  • S s907461903
                    9 Feb 2023, 09:56

                    @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

                    J Offline
                    J Offline
                    JonB
                    wrote on 9 Feb 2023, 09:59 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 9 Feb 2023, 10:06
                    0
                    • J JonB
                      9 Feb 2023, 09:59

                      @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 9 Feb 2023, 10:06 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 J 2 Replies Last reply 9 Feb 2023, 11:17
                      0
                      • S s907461903
                        9 Feb 2023, 10:06

                        @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 9 Feb 2023, 11:17 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 10 Feb 2023, 00:24
                        0
                        • S s907461903
                          9 Feb 2023, 10:06

                          @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" ...
                          
                          J Offline
                          J Offline
                          JonB
                          wrote on 9 Feb 2023, 12:01 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 10 Feb 2023, 00:44
                          0
                          • SGaistS SGaist moved this topic from General and Desktop on 9 Feb 2023, 20:05
                          • jsulmJ jsulm
                            9 Feb 2023, 11:17

                            @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 10 Feb 2023, 00:24 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
                            • J JonB
                              9 Feb 2023, 12:01

                              @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 10 Feb 2023, 00:44 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 10 Feb 2023, 03:34
                              0
                              • S s907461903
                                10 Feb 2023, 00:44

                                @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 10 Feb 2023, 03:34 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