How to automatic update the data in QTableWidget from mysql?

76
September 19, 2021, at 12:00 PM

I'm working for a simple project and I need a way to update the QTablewidget below automatically if there's new data inserted or deleted

import sys
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5 import QtWidgets, uic
import mysql.connector

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__() # Call the inherited classes __init__ method
        uic.loadUi('home.ui', self) # Load the .ui file
        self.show() # Show the GUI
        self.tableWidget.setColumnWidth(0,250)
        self.tableWidget.setColumnWidth(1,100)
        self.tableWidget.setColumnWidth(2,200)
        self.loaddata()
#database
 
    def loaddata(self):
        mydb = mysql.connector.connect(
        host="localhost",
        user="root",
        password="",
        database="face_attendancesystem" )
        cursor = mydb.cursor()
        cursor.execute("SELECT * from known_faces")
        myresult = cursor.fetchall()
        tablerow=0
        self.tableWidget.setRowCount(40)

        for row in myresult:
            self.tableWidget.setItem(tablerow, 0, QtWidgets.QTableWidgetItem(row[1]))
            self.tableWidget.setItem(tablerow, 1, QtWidgets.QTableWidgetItem(row[2]))
            self.tableWidget.setItem(tablerow, 2, QtWidgets.QTableWidgetItem(row[3]))
            tablerow+=1

app = QtWidgets.QApplication(sys.argv) # Create an instance of QtWidgets.QApplication
window = Ui() # Create an instance of our class
try:
    sys.exit(app.exec_())
except:
    print("Exiting")

I'm using PyQT5 and MySQL for my database, is there a way to do it?

READ ALSO
React web based app connect to physcal hardware devices

React web based app connect to physcal hardware devices

I don't know is the right place or not to ask this questionI have a web-based inventory application that is running on Ipad

54
Postgresql's QueryExecutor hanging indefinitely at `receiveChar()` for query which has successfully completed

Postgresql's QueryExecutor hanging indefinitely at `receiveChar()` for query which has successfully completed

I'm facing a weird issue where Postgres seemingly isn't sending a reply to my client code even after successfully completing the queryAs such, it hangs around indefinitely in the blocking receiveChar() call waiting for something that never arrives

81
paste event not loading data in the list for first time paste ,but working fine after that

paste event not loading data in the list for first time paste ,but working fine after that

whenever the paste event happens in input box it does not load options in data-list in first attempt but in second attempts it works and enables the button to proceed furtherAlso it works fine in keyup event

73
Model as a variable

Model as a variable

I use switch to execute the code I want depending on the modelIs there a way to immediately substitute the model into the code from the variable that comes to me to avoid repeating the code 3 times?

52