pyqt5 - Using QSqlTableModel with MySQL connector
-
wrote on 15 Mar 2022, 16:14 last edited by
I have a form with QtableView widget: I would like to load data from a MySQL table using QSqlTableModel.
I'm trying with MySQL connector but it seems not working
Below, is the piece of the code where the model is set.
Thanks for any helpimport mysql.connector from PyQt5.QtSql import QSqlDatabase, QSqlTableModel class MainWindow(QWidget, Ui_f_tabella): def __init__(self): super().__init__() self.setupUi(self) Database.connection = mysql.connector.connect(host="localhost", user="xxxxxxxxx", password="xxxxxxxxxxxx", database="db_m") Database.cursor = Database.connection.cursor() self.model = QSqlTableModel(self) self.model.setTable("tb_RisorseInterne") self.model.setEditStrategy(QSqlTableModel.OnFieldChange) self.model.select()
-
You can not mix mysql.connector and QtSql classes - you have to use QSqlDatabase for this.
Note: there is no mysql plugin shipped by default with Qt (due to stupid mysql license stuff) - you may have to compile it by yourself.
1/2