with sqlite3.connect('database.db') as db:
cursor=db.cursor()
cursor.execute('select* from Item Order BY Name ASC')
title = [cn[0] for cn in cursor.description]
rows = [cn[0] for cn in cursor.description]
cur=cursor.fetchall()
layout = QtGui.QGridLayout()
self.test = QtGui.QLineEdit('test') #This is just a test for printing text into the table and works fine.
self.table = QtGui.QTableWidget()
for rows in cur:
print(rows[1]) #This is just a test to see if the data could be printed into python shell, which worked.
#self.test2 = QtGui.QLineEdit(cur)
#self.table.setVerticalHeaderLabels(rows)
self.table.setRowCount(3)
self.table.setColumnCount(5)
self.table.setHorizontalHeaderLabels(title) #This code works fine, the column headers are the ones from my database.
#self.table.setVerticalHeaderItem(1,row)
layout.addWidget(self.table, 0, 0)
self.table.setItem(0, 0, QtGui.QTableWidgetItem(self.test.text()))
#self.table.setItem(1, 0, QtGui.QTableWidgetItem(self.test2.text()))
self.setLayout(layout)
哈希代码是我玩过的区域,但是导致错误,或者只是不工作。我附上了表的所有代码,以便更容易理解我想要完成的任务。
该程序当前打开一个表,其中包含数据库中的列标题,其中包含3个空行(不包括测试)。我想用我的sqlite数据库中的值自动填充这些行。如果您需要了解任何信息,请询问。
答案 0 :(得分:1)
您可以这样做:
cur=cursor.fetchall()
for i,row in enumerate(cur):
for j,val in enumerate(row):
table.setItem(i, j, QtGui.QTableWidgetItem(str(val)))