[英]How to display 2 SQLite tables with one to many relation into one QTableWidget
I have following two tables in Db, with one to many relationship: 我在Db中有以下两个表,具有一对多关系:
self.c.execute("CREATE TABLE IF NOT EXISTS dispatch("
"id_dispatch INTEGER PRIMARY KEY AUTOINCREMENT,"
"d_date TEXT,"
"d_mill TEXT,"
"d_buyer TEXT,"
"d_addFreight INTEGER,"
"d_subFreight INTEGER,"
"d_amount INTEGER,"
"d_cd INTEGER,"
"d_comm INTEGER,"
"d_netAmount INTEGER)")
self.c.execute("CREATE TABLE IF NOT EXISTS dispatch_products("
"dispatch_id INTEGER NOT NULL REFERENCES DISPATCH(id_dispatch),"
"product INTEGER,"
"quantity INTEGER,"
"rate INTEGER)")
I'm trying to combine these two table into one and display in one table, using following code: 我正在尝试使用以下代码将这两个表合并为一个表并在一个表中显示:
def loadTable(self):
connection = sqlite3.connect('main_databaseSample.db')
query = "CREATE VIEW myview as SELECT d_buyer," \
"d_date, product, quantity, rate, d_amount, d_addFreight, d_subFreight, d_netAmount from " \
"dispatch, dispatch_products WHERE dispatch.id_dispatch = dispatch_products.dispatch_id"
query = "SELECT * FROM myview"
result = connection.execute(query)
self.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate(row_data):
self.tableWidget.setItem(row_number, column_number,QtWidgets.QTableWidgetItem(str(data)))
connection.close()
PROBLEM : Orders which have multiple products are repeated. 问题 :具有多个产品的订单被重复。 How to eliminate multiple entries which have one to many relationship? 如何消除具有一对多关系的多个条目?
Sample Data: 样本数据:
dispatch table:
ID date mill buyer addF subF amount cd comm netAmount
1 15-10 abc A 0 0 100 0 0 100
2 16-10 xyz B 0 0 200 0 0 200
dispatch_products table:
Dispatch_ID product qty rate
1 M 40 1
1 A 60 1
2 S 50 4
Code Output:
buyer date product quantity rate amount addFreight subFreight NetAmount
A 15-10 M 40 1 100 0 0 100
A 15-10 A 60 1 100 0 0 100
B 16-10 S 50 4 200 0 0 200
Expected Output:
buyer date product quantity rate amount addFreight subFreight NetAmount
A 15-10 M 40 1 100 0 0 100
A 60 1
B 16-10 S 50 4 200 0 0 200
The task is to detect that there are equal elements for it we must add one more element to the View, the dispatch_id or id_dispatch and get them sorted. 任务是检测是否有相等的元素,我们必须在视图中再添加一个元素,dispatch_id或id_dispatch并对其进行排序。 After detecting the change of dispatch_id join the necessary rows using setSpan()
as shown below: 在检测到dispatch_id的更改之后,使用setSpan()
加入必要的行,如下所示:
from PyQt5 import QtCore, QtWidgets
import sqlite3
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
button = QtWidgets.QPushButton("Press me")
button.clicked.connect(self.load_table)
self.tableWidget = QtWidgets.QTableWidget()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(button)
lay.addWidget(self.tableWidget)
@QtCore.pyqtSlot()
def load_table(self):
self.tableWidget.setRowCount(0)
self.tableWidget.setColumnCount(9)
connection = sqlite3.connect('main_databaseSample.db')
connection.execute("DROP VIEW IF EXISTS myview;")
query = "CREATE VIEW myview as SELECT dispatch_id, d_buyer," \
"d_date, product, quantity, rate, d_amount, d_addFreight, d_subFreight, d_netAmount from " \
"dispatch, dispatch_products WHERE dispatch.id_dispatch = dispatch_products.dispatch_id " \
"ORDER BY dispatch_id ASC;"
connection.execute(query)
query = "SELECT * FROM myview"
result = connection.execute(query)
last_id = -1
start_row = 0
for row, row_data in enumerate(result):
self.tableWidget.insertRow(row)
current_id, *other_values = row_data
for col, data in enumerate(other_values):
it = QtWidgets.QTableWidgetItem(str(data))
self.tableWidget.setItem(row, col, it)
if last_id != current_id and last_id != -1:
self.apply_span(start_row, row - start_row)
start_row = row
last_id = current_id
if start_row != row:
self.apply_span(start_row, self.tableWidget.rowCount() - start_row)
def apply_span(self, row, nrow):
if nrow <= 1:
return
for c in (0, 1, 5, 6, 7, 8):
self.tableWidget.setSpan(row, c, nrow, 1)
for r in range(row+1, row+nrow):
t = self.tableWidget.takeItem(r, c)
del t
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Adding more data we can see the following result 添加更多数据,我们可以看到以下结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.