PDA

View Full Version : Reload function doesn't catch updates on database , closes when no data



litoo00
6th July 2018, 11:24
My code is working great but it should reload the Qwidget to get the new data from DB every 1 min.

Also, when I click the button to move to the 2nd UI then I submit back . if there is no data to show on the Qtable widget it simply ends the program.

Here is my code :




# -*- coding: utf-8 -*-

from PyQt4 import QtGui, QtCore
import sys
import main3, report
import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="Buii3tf0ky001@yahoo000", # your password
db="health", # name of the database
port=3306) # number of the data base port

cur = db.cursor()


class report(QtGui.QMainWindow, report.Ui_Report1):
def __init__(self, p_id, parent=None):
super(report, self).__init__(parent)
self.setupUi(self)

self.lineEdit.setText('ID: ' + str(p_id))
cur.execute("SELECT * FROM patient WHERE id = '%s'" % (p_id))
pt_row_report = cur.fetchall()
for row in pt_row_report:
pt_name_report = row[1]
pt_age_report = row[2]
cur.execute("SELECT * FROM clinic WHERE p_id = '%s'" % (p_id))
clinic_row = cur.fetchone()
d_id = clinic_row[2]
ray_type1 = clinic_row[6]
ray_type = ray_type1[17:]
cur.execute("SELECT * FROM staff WHERE id = '%s'" % (d_id))
staff_row = cur.fetchall()[0]
doctor_name = staff_row[1]
doctor_id = staff_row[0]
self.lineEdit_2.setText('Name: ' + str(pt_name_report))
self.lineEdit_3.setText('Age: ' + str(pt_age_report))
self.lineEdit_6.setText(str(ray_type))
self.lineEdit_7.setText('Dr. ' + str(doctor_name))

cur.execute("SELECT Name FROM staff WHERE id = 48588")
ray_dr_name = cur.fetchone()[0]
self.lineEdit_4.setText('Dr. ' + str(ray_dr_name))
self.pushButton.clicked.connect(lambda: self.update_report(p_id, ray_type, doctor_id))

def closeEvent(self, event):
First().show()
event.accept()

def update_report(self, name, ray, d_id):

x = self.textEdit.toPlainText()
sql = "UPDATE clinic SET Extra_Notice = (%s) WHERE p_id=(%s)"
cur.execute(sql, (x, name))
db.commit()
cur.execute("INSERT INTO rays (p_ID, Ray_Type, Report, D_ID, RS_ID) VALUES (%s,%s,%s,%s,%s)",
(int(name), ray, x, d_id, 48588))
db.commit()
self.hide()
First().reload_data()




def get_text(self, x):
x = self.textEdit.text()
return x


class First(QtGui.QMainWindow, main3.Ui_MainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.setupUi(self)
timer = QtCore.QTimer(self)
timer.timeout.connect(self.reload_data)
timer.start(60 * 1000)
self.reload_data()

def reload_data(self):
self.show()
self.tableWidget.clear()
self.tableWidget.setRowCount(0)
self.tableWidget.setColumnCount(7)
self.tableWidget.setHorizontalHeaderLabels(
("Queue number;ID;Name;Age;Address;Phone;Make Operation;").split(";"))
header = self.tableWidget.horizontalHeader()
header.setResizeMode(0, QtGui.QHeaderView.Stretch)
header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)

cur.execute("SELECT * FROM clinic WHERE Extra_Notice LIKE %s", ("%{}%".format('x-ray is required'),))
test_list = cur.fetchall()
queue = 1
for row in test_list:
c_id = row[0]
p_id = row[1]
cur.execute("SELECT * FROM patient WHERE id = '%s'" % (p_id))
pt = cur.fetchone()
print(pt)
if not pt:
continue
pt_name = pt[1]
pt_age = pt[2]
pt_address = pt[4]
pt_phone = pt[3]

button = QtGui.QPushButton('Make Operation')
button.setStyleSheet("background-color: #4f81bc; color: white;")
button.clicked.connect(lambda checked, p_id=p_id: self.open_report(p_id))

rowposition = self.tableWidget.rowCount()

self.tableWidget.insertRow(rowposition)

for i, val in enumerate([queue, c_id, pt_name, pt_age, pt_address, pt_phone]):
self.tableWidget.setItem(rowposition, i, QtGui.QTableWidgetItem(str(val)))
self.tableWidget.setCellWidget(rowposition, 6, button)
queue += 1

def open_report(self, p_id):
self.child_win = report(p_id)
self.child_win.show()
self.hide()


def main():
app = QtGui.QApplication(sys.argv)
app.setStyle('Plastique')
main = First()
main.update()
main.show()
sys.exit(app.exec_())


if __name__ == '__main__':
main()


Pleae keep in my mind that am using pyqt4 with python 2.7

d_stranz
8th July 2018, 22:17
I am not a python expert, but aren't lines 48 and 61 creating -new- instances of First (your main window) when you should be using the single instance you create in main()?