我试图在PyQt4的TreeWidget中查看一个简单的MySQL-SELECT。我能够访问我当前的数据库,并能够打印结果。但我不知道它在TreeWidget中对我有用。
我找到了一些文档和教程,但没有一个真正有用。我希望你们中的一些人可以帮助我。
这是我的代码:
def SqlConnectionTest(self):
try:
cnn = mysql.connector.connect(
user = 'root',
host = 'localhost',
database = 'employeedb')
self.ui.textBrowser_exceptionDisplay.setText("Connection works!")
cursor = cnn.cursor()
cursor.execute("SELECT * `from employeeinfo_table`;")
results = cursor.fetchall()
for row in results:
print(row)
#self.ui.treeWidget_EmployeeList.addTopLevelItems()
if cnn:
cnn.close()
except mysql.connector.Error as e:
if e.errno == errorcode.ER_ACCESS_DENIED_ERROR:
self.ui.textBrowser_exceptionDisplay.setText("Error. Check your username or password!")
elif e.errno == errorcode.ER_BAD_DB_ERROR:
self.ui.textBrowser_exceptionDisplay.setText("Database is not available or does not exist!")
else:
self.ui.textBrowser_exceptionDisplay.setText(e)
目前我没有向treeWidget添加任何项目,因为我不知道除了这种方式。这只是测试。我知道这不是好事(Mitarbeiter(DE)=员工(EN)):
item = QtGui.QTreeWidgetItem([emp1.displayEmployeeName()])
child = QtGui.QTreeWidgetItem(["Details"])
child1 = QtGui.QTreeWidgetItem([emp1.displayFullEmployeeInfo()])
child2 = QtGui.QTreeWidgetItem(["Mitarbeiter"])
child3 = QtGui.QTreeWidgetItem([emp2.displayEmployeeName()])
child4 = QtGui.QTreeWidgetItem(["Details"])
child5 = QtGui.QTreeWidgetItem([emp2.displayFullEmployeeInfo()])
child6 = QtGui.QTreeWidgetItem([emp3.displayEmployeeName()])
child7 = QtGui.QTreeWidgetItem(["Details"])
child8 = QtGui.QTreeWidgetItem([emp3.displayFullEmployeeInfo()])
child9 = QtGui.QTreeWidgetItem(["Mitarbeiter"])
child10 = QtGui.QTreeWidgetItem([emp4.displayEmployeeName()])
child11 = QtGui.QTreeWidgetItem(["Details"])
child12 = QtGui.QTreeWidgetItem([emp4.displayFullEmployeeInfo()])
item.addChild(child)
child.addChild(child1)
child.addChild(child2)
child2.addChild(child3)
child3.addChild(child4)
child4.addChild(child5)
child2.addChild(child6)
child6.addChild(child7)
child7.addChild(child8)
child7.addChild(child9)
child9.addChild(child10)
child10.addChild(child11)
child11.addChild(child12)
self.ui.treeWidget_EmployeeList.addTopLevelItem(item)