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. pyqt5 - Using QSqlTableModel with MySQL connector
Forum Updated to NodeBB v4.3 + New Features

pyqt5 - Using QSqlTableModel with MySQL connector

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 574 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.
  • A Offline
    A Offline
    Achab61
    wrote on 15 Mar 2022, 16:14 last edited by
    #1

    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 help

       import 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()
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 15 Mar 2022, 16:24 last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1

      1/2

      15 Mar 2022, 16:14

      • Login

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