我正在使用Python / PyQt5中的小型搜索应用程序工作,该应用程序浸入远程SQL db,将结果发送到本地db文件,然后将来自db文件的行输出到QTableWidget对象。该应用程序运行,创建本地db文件,但QTableWidget对象中未显示任何内容。我分别测试了将行插入QTableWidget的代码部分,它确实起作用。所以我不太确定下一步要寻找什么。PS。我知道总体设计很麻烦,但是我是Python的新手,这是我能想到的最好的方法。
def bttn_search (self):
# Set initialization
Set_IncidentNumber_CustID = set()
Set_IncidentNumber_Priority = set()
Set_IncidentNumber_Service = set()
Set_IncidentNumber_MIN = set()
Set_IncidentNumber_CreatedBy = set()
Set_Company_CustID_Substring = set()
Company_CustID = self.ui.Input_lineEdit_PIN.text()
Incident_Priority = self.ui.Input_lineEdit_Priority.text()
Incident_Service = self.ui.Input_lineEdit_Service.text()
Incident_MIN = self.ui.Input_lineEdit_MIN.text()
Incident_CreatedBy = self.ui.Input_lineEdit_Owner.text()
FoundRecID = -1
with pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=server_address;DATABASE=myremotedb;UID=username;PWD=password') as DB_Connect:
SearchCursor = DB_Connect.cursor()
SearchCursor.execute("select RecId, CustID, Name from Company")
for row in SearchCursor.fetchall():
if str(row[1]).lower() == str(Company_CustID).lower() and len(str(Company_CustID)) == 6:
FoundRecID = row[0]
if str(row[1]).lower().rfind(str(Company_CustID).lower()) != -1 and len(str(Company_CustID)) > 1:
Set_Company_CustID_Substring.add(row[0])
SearchCursor.execute("select IncidentNumber, CompanyLink_RecID, Priority, Service, CellNum, Owner from Incident")
for row in SearchCursor.fetchall():
if row[1] == FoundRecID:
Set_IncidentNumber_CustID.add(int(row[0])) # Append CustID set with the value of every instance of CompanyLink_RecID from the Incident table
if row[2] == Incident_Priority:
Set_IncidentNumber_Priority.add(int(row[0])) # Append Priority set with the value of every instance of Priority from the Incident table
if (row[3].lower()).rfind(Incident_Service.lower()) != -1 and len(Incident_Service) > 3:
Set_IncidentNumber_Service.add(int(row[0]))
if row[4] == Incident_MIN:
Set_IncidentNumber_MIN.add(int(row[0])) # Append MIN set with the value of every instance of Phone from the Incident table
if str(row[4]).rfind(str(Incident_MIN)) != -1 and len(str(Incident_MIN)) > 2:
Set_IncidentNumber_MIN.add(int(row[0]))
if str(row[4]).replace("-","") == str(Incident_MIN).replace("-", ""):
Set_IncidentNumber_MIN.add(int(row[0]))
if str(row[5]).rfind(str(Incident_CreatedBy)) != -1 and len(str(Incident_CreatedBy)) > 1:
Set_IncidentNumber_CreatedBy.add(int(row[0])) # Append MIN set with the value of every instance of Phone from the Incident table
for Set_Company_CustID_Substring_Item in Set_Company_CustID_Substring:
if row[1] == Set_Company_CustID_Substring_Item:
Set_IncidentNumber_CustID.add(int(row[0]))
List_Of_All_Sets = [Set_IncidentNumber_Priority, Set_IncidentNumber_CustID, Set_IncidentNumber_Service,Set_IncidentNumber_MIN, Set_IncidentNumber_CreatedBy]
try:
Set_Intersection = set.intersection(*(s for s in List_Of_All_Sets if s))
except TypeError:
alert = QMessageBox()
alert.setText('No results')
alert.exec_()
else:
DB_Local_Filename = time.strftime("%Y%m%d%H%M%S.db")
DB_Connect_Local = sqlite3.connect(DB_Local_Filename)
DB_Local_Cursor = DB_Connect_Local.cursor()
DB_Local_Cursor.execute("""
CREATE TABLE TableResults
(IncidentNumber TEXT, CustID TEXT, Priority TEXT, Service TEXT, CellNum TEXT, Owner TEXT)
""")
DB_Connect_Local.commit()
DB_Connect_Local_Insert_Command = ("INSERT INTO TableResults(IncidentNumber, CustID, Priority, Service, CellNum, Owner) VALUES (?, ?, ?, ?, ?, ?)")
DB_Local_List_Insert = list()
with pyodbc.connect(
'DRIVER={ODBC Driver 17 for SQL Server};SERVER=server_address;DATABASE=myremotedb;UID=username;PWD=password') as DB_Connect:
Set_Intersection_Cursor = DB_Connect.cursor()
Set_Intersection_Cursor.execute("select CompanyLink_RecID, IncidentNumber Name from Incident")
for Set_Intersection_Cursor_Incident_Item in Set_Intersection_Cursor.fetchall():
for Set_Intersection_Item in Set_Intersection:
if Set_Intersection_Cursor_Incident_Item[1] == Set_Intersection_Item:
FoundRecID = Set_Intersection_Cursor_Incident_Item[0]
Set_Intersection_Cursor.execute("select RecId, CustID, Name from Company")
for Set_Intersection_Cursor_Company_Item in Set_Intersection_Cursor.fetchall():
if Set_Intersection_Cursor_Company_Item[0] == FoundRecID:
DB_Local_List_Insert.insert(0, Set_Intersection_Item)
DB_Local_List_Insert.insert(1, Set_Intersection_Cursor_Company_Item[1]) # Append the Row to be inserted in the Local DB with the CustID (PIN) associated to the IncidentNumber (Set_Intersection_Item)
Set_Intersection_Cursor.execute("select IncidentNumber, Priority, Service, CellNum, Owner from Incident")
for row in Set_Intersection_Cursor.fetchall():
if row[0] == Set_Intersection_Item:
DB_Local_List_Insert.insert(2, int(row[1]))
if row[0] == Set_Intersection_Item:
DB_Local_List_Insert.insert(3, row[2])
if row[0] == Set_Intersection_Item:
DB_Local_List_Insert.insert(4, row[3])
if row[0] == Set_Intersection_Item:
DB_Local_List_Insert.insert(5, row[4])
DB_Local_Cursor.execute(DB_Connect_Local_Insert_Command, DB_Local_List_Insert)
DB_Connect_Local.commit()
#print(DB_Local_List_Insert)
DB_Local_List_Insert.clear()
#DB_Local_List_Insert = list()
alert = QMessageBox()
alert.setText('Search has completed, now populating results')
alert.exec_()
db_local_tableWidget = QTableWidget()
DB_Connect_Local = sqlite3.connect(DB_Local_Filename)
DB_Local_Cursor = DB_Connect_Local.cursor()
DB_Connect_Local_Select_All = "SELECT * FROM TableResults"
DB_Connect_Local_Show_All = DB_Local_Cursor.execute(DB_Connect_Local_Select_All)
db_local_tableWidget.setRowCount(200)
db_local_tableWidget.setColumnCount(6)
for row_number, row_data in enumerate(DB_Connect_Local_Show_All):
db_local_tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
db_local_tableWidget.setItem(row_number, column_number, QTableWidgetItem(str(data)))
alert = QMessageBox()
alert.setText('Results have been populated')
alert.exec_()
DB_Connect_Local.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = pySearchMain()
main.show()
sys.exit(app.exec_())
诚然,我对QT的经验不多,但这是我填充QTableWidget
的方式:
((我已经根据您的示例调整了代码)
我使用QT设计器构建包含4列的QTableWidget
的表单:然后我像这样一一添加行:
# add row
row_num = db_local_tableWidget.rowCount()
db_local_tableWidget.insertRow(row_num)
db_local_tableWidget.setItem(row_num , 0, QTableWidgetItem(value1))
db_local_tableWidget.setItem(row_num , 1, QTableWidgetItem(value2))
db_local_tableWidget.setItem(row_num , 2, QTableWidgetItem(value3))
db_local_tableWidget.setItem(row_num , 3, QTableWidgetItem(value4))
db_local_tableWidget.resizeColumnsToContents()
我不认为您应该这样做:
db_local_tableWidget.setRowCount(200)
db_local_tableWidget.setColumnCount(6)
如果您事先不知道将获得多少行,那很好。在阅读数据时只需添加它们即可。
我还没有检查其余的代码,但是您应该添加一些调试输出,或者使用logger
类登录到控制台或文件。首先,请确保实际上已执行带有双for loop
的代码,并且值,偏移量等正确。您应该能够轻松解决。如果代码按预期运行,但网格未更新,则这是另一个问题。