Search code examples
pyqt4

How to display count of data onto the label using pyqt


self.pushButton786.clicked.connect(self.refresh)

def refresh(self):

    db = MySQLdb.connect('localhost', 'root', 'Suhel786', 'project')
    cursor = db.cursor()
    a=cursor.execute("select count(*) from donor")
    b=cursor.execute("select count(*) from customer")
    c=str(a)
    d=str(b)
    self.label_8.setText(c)
    self.label_9.setText(d)
    db.close()

this Gives always 1 even if there is no data in database...


Solution

  • self.pushButton786.clicked.connect(self.refresh)
    
    def refresh(self):
    
        db = MySQLdb.connect('localhost', 'root', 'Suhel786', 'project')
        cursor = db.cursor()
        cursor.execute("select count(*) from donor")
        a=fetchchone()
        b=int(a[0])
        cursor.execute("select count(*) from customer")
        c=fetchchone()
        d=int(c[0])
        self.label_8.setText(str(b))
        self.label_9.setText(str(d))
        db.close()