Database is PostgreSQL
ImportSYS fromFormImportUi_form fromPyqt5.qtImportQwidget, Qapplication,qtablewidgetitemImportPSYCOPG2classMyForm (qwidget,ui_form):def __init__(self): Super ().__init__() self.setupui (self) self.btn1.clicked.connect (self.clear) self.btn2.clicked.connect (sel F.load) self.show ()defClear (self):Pass defload (self): conn=psycopg2.connect ("dbname=test1_data USER=JM password=123") cur=conn.cursor () Cur.execute ('SELECT * FROM table1') Rows=Cur.fetchall () row=Cur.rowcount #取得记录个数 for setting the number of rows in a table vol=Len (rows[0]) #取得字段数, sets the number of columns in the table Cur.close () conn.close () Self.table.setRowCount (Row) Self.table.setColumnCount (vol) forIinchrange (Row): forJinchRange (vol): Temp_data=Rows[i][j] #临时记录, cannot insert table data directly=Qtablewidgetitem (str (temp_data)) #转换后可插入表格 Self.table.setItem (i,j,data) app=qapplication (SYS.ARGV) W=MyForm () app.exec_ ( )
PYQT5 database data is displayed in the table