使用“ Dynamic”过滤带有SQL数据的Python表格SQL查询
我有一个带有表格的PYQT5对话框,3个按钮(删除行,添加行和过滤器)和5个Commoboxes,在这些ComboBoxes中,我将所有项目写在外部SQL数据库中(例如,我有客户,drivers ecc ..)。 ,用户可以从每个Combobox中选择一个项目,当用户按过滤器按钮(对象名称为“ BottoneFiltra')时,AFETR会自动选择该表,该表将根据哪个客户,驱动程序ECC进行过滤。这是我的代码,这是我卡住的地方:
def __init__(self):
super(modificaviaggi, self).__init__()
uic.loadUi('modifica_reg.ui', self)
bottonefiltra = self.bottone_filtra
bottonefiltra.clicked.connect(self.filtertable)
def filtertable(self):
self.tabella_registrazioni.clear()
self.tabella_registrazioni.blockSignals(True)
startdate = self.input_dal.date().toPyDate()
enddate = self.input_al.date().toPyDate()
farticle = self.filtro_articolo.currentText()
fwarehouse = self.filtro_magazzino.currentText()
fcustomer = self.filtro_cliente.currentText()
fplate = self.filtro_targa.currentText()
fdriver = self.filtro_autista.currentText()
如何创建一个基于所选项目而不创建30个语句的项目更改的查询?我会更好地解释它,做我想做的事情,我很可能会创建一个IF检查哪个ComboBox的项目选择了这样的项目:
if farticle != " " :
if fwarehouse != " ":
if fcustomer != " " :
if fplate != " " :
if fdriver != " " :
然后,我会在其中执行一个查询:
c.execute('SELECT * FROM Viaggi WHERE DataDDT < ? AND DataDDT > ? AND Ragione_sociale = ? AND Targa = ? AND Autista = ? AND Magazzino = ? AND Articolo = ?',
(fcustomer, fplate, fdriver, fwarehouse, farticle))
但是每次我都必须像10-- 15基于选择哪些项目的组合的不同查询(也许1个组合选择了一个项目,第三个没有ECC ...)。如果在不编写20个查询的情况下,我该怎么做,而在不进行20个查询的情况下,如果语句要检查哪个组合已选择一个项目?
I have an PyQt5 dialog with a table, 3 buttons (delete row, add row and filter) and 5 comboboxes, in those comboboxes I have all the items written in an external SQL databes (for example I have customers, drivers ecc..), the user can choose an item from each combobox, afetr choosing it when the users press the filter button(the object name is 'bottonefiltra') automatically the table will be filtered based on which customer, driver ecc the user selected. Here's my code and here's where I'm stuck :
def __init__(self):
super(modificaviaggi, self).__init__()
uic.loadUi('modifica_reg.ui', self)
bottonefiltra = self.bottone_filtra
bottonefiltra.clicked.connect(self.filtertable)
def filtertable(self):
self.tabella_registrazioni.clear()
self.tabella_registrazioni.blockSignals(True)
startdate = self.input_dal.date().toPyDate()
enddate = self.input_al.date().toPyDate()
farticle = self.filtro_articolo.currentText()
fwarehouse = self.filtro_magazzino.currentText()
fcustomer = self.filtro_cliente.currentText()
fplate = self.filtro_targa.currentText()
fdriver = self.filtro_autista.currentText()
How can I create a query that changes based on the item selected without creating 30 if statements? I'll explain it better, to do what I want I'd most likely create an if to check which combobox has an item selected like this :
if farticle != " " :
if fwarehouse != " ":
if fcustomer != " " :
if fplate != " " :
if fdriver != " " :
then I'll execute a query where like :
c.execute('SELECT * FROM Viaggi WHERE DataDDT < ? AND DataDDT > ? AND Ragione_sociale = ? AND Targa = ? AND Autista = ? AND Magazzino = ? AND Articolo = ?',
(fcustomer, fplate, fdriver, fwarehouse, farticle))
but everytime I have to write like 10-15 different queries based on the combination of which items are selected(maybe 1 combo has an item selected and third no ecc...). How can I do it without writing 20 queries and without making 20 If statements to check which combo has an item selected ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能性是将每个组合与字段名称配对,然后循环通过它们构建查询和值列表。
__ Init __
在其中创建一个与这些对的列表中的某个地方:然后遍历它并构建查询:
A possibility would be to pair each combo with a field name, then cycle through them to build both the query and the value list.
Somewhere in the
__init__
create a list with those pairs:Then iterate through it and build the query: