Mac Pyside6 QSqlDatabase: QMYSQL driver not loaded
-
wrote on 15 Aug 2022, 06:21 last edited by
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 -
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 openwrote on 15 Aug 2022, 08:02 last edited by JonB@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 afterapp = QApplication(sys.argv)
. I suggest you look at some of the examples and follow them. -
wrote on 16 Aug 2022, 05:25 last edited by
After compiling the mysql driver through the source code, the problem has been solved, thank you for your suggestion.
-
After compiling the mysql driver through the source code, the problem has been solved, thank you for your suggestion.
wrote on 8 Feb 2023, 09:31 last edited by@jerry0305 How do you compile the mysql driver for pyside6? Could you please share your compliation progress with me when you are convenient?
-
@jerry0305 How do you compile the mysql driver for pyside6? Could you please share your compliation progress with me when you are convenient?
wrote on 8 Feb 2023, 09:36 last edited by@s907461903
The driver must be compiled with C++ before it can be used from Python. -
@s907461903
The driver must be compiled with C++ before it can be used from Python.wrote on 9 Feb 2023, 09:34 last edited by@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())
-
@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())
wrote on 9 Feb 2023, 09:38 last edited by@s907461903
In a terminal/shell set an environment variableQT_DEBUG_PLUGINS=1
and then run your Python script/application. You should get diagnostic output for why it can't find the QMYSQL driver. -
@s907461903
In a terminal/shell set an environment variableQT_DEBUG_PLUGINS=1
and then run your Python script/application. You should get diagnostic output for why it can't find the QMYSQL driver.wrote on 9 Feb 2023, 09:56 last edited by@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:
-
@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:
wrote on 9 Feb 2023, 09:59 last edited by@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 theQMYSQL
driver. -
@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 theQMYSQL
driver.wrote on 9 Feb 2023, 10:06 last edited by@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" ...
-
@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" ...
@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? -
@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" ...
wrote on 9 Feb 2023, 12:01 last edited by@s907461903
EitherC:\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 SGaist moved this topic from General and Desktop on 9 Feb 2023, 20:05
-
@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?wrote on 10 Feb 2023, 00:24 last edited by@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.
-
@s907461903
EitherC:\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.wrote on 10 Feb 2023, 00:44 last edited by@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.
-
@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.
wrote on 10 Feb 2023, 03:34 last edited by@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.