python - 有没有办法根据列表的长度设置 SQL 查询中的变量数量?
问题描述
我正在尝试根据用户的选择从数据库中获取每月值并将它们可视化。我有复选框,如下所示;
为了根据用户的选择获取每月销售数字,我从复选框中获取值并创建一个仅包含不等于零的值的新列表。之后,我编写了一个 Select 查询,其中特定列具有列表中的任何值。但是用户只能选择 1 个值或 3 个值或其他任何值,我的 sql 查询必须根据它进行更改。如果用户选择 1 个值,如果他们选择 5 个值,我必须在查询中只使用 1 个“%s”,我必须使用其中的 5 个。所以我创建了一堆 if 函数,其中条件是列表的长度。有没有办法根据列表的长度来安排 sql 查询中的变量数量?
def plot(self):
self.listo=[]
for val in self.lili:
# self.lili is a list that has all of the checkboxes value in it. Unchecked box values is 0 for all.Checked box values is changing based on month.
val=val.get()
if val !=0:# I created a empty list and add all the checked values in it.
self.listo.append(val)
else:
pass
self.tryn=len(self.listo)# I get the lenght of checked checkboxes.
if self.tryn==0:#If anything is selected it's an error.
messagebox.showerror("Error", "You have to choose at least one")
if self.tryn==1:##If 1 values is selected use this query with 1 variable.
self.cursor.execute("SELECT SUM(current_sales) FROM targets WHERE nameofmonth=(%s)",(self.listo[0],))
self.current_sales1=self.cursor.fetchall()[0][0]
self.cursor.execute("SELECT SUM(target_sales) FROM targets WHERE nameofmonth=(%s)",(self.listo[0],))
self.target_sales1=self.cursor.fetchall()[0][0]
self.show_plot()
if self.tryn==2:##If 2 values is selected use this query with 2 variable.
query=("SELECT SUM(target_sales) FROM targets WHERE (nameofmonth=(%s) OR nameofmonth=(%s))")
self.cursor.execute(query,self.listo)
self.target_sales1=self.cursor.fetchall()[0][0]
query2=("SELECT SUM(current_sales) FROM targets WHERE (nameofmonth=(%s) OR nameofmonth=(%s))")
self.cursor.execute(query2,self.listo)
self.current_sales1=self.cursor.fetchall()[0][0]
self.show_plot()
if self.tryn==3:
query=("SELECT SUM(target_sales) FROM targets WHERE (nameofmonth=(%s) OR nameofmonth=(%s)) OR nameofmonth=(%s)")
self.cursor.execute(query,self.listo)
self.target_sales1=self.cursor.fetchall()[0][0]
query2=("SELECT SUM(current_sales) FROM targets WHERE (nameofmonth=(%s) OR nameofmonth=(%s)) OR nameofmonth=(%s)")
self.cursor.execute(query2,self.listo)
self.current_sales1=self.cursor.fetchall()[0][0]
self.show_plot()
解决方案
我找到了一个更好的方法来做到这一点。我创建了一个字符串并将其与包含选中复选框值的列表的长度相乘。
query="WHERE "+"nameofmonth=(%s) OR "*self.tryn
为了摆脱最后的额外 OR 和空间;
query=query[:-3]
在和我刚刚使用这个变量格式化我的查询。
你可以在这里看到完整的功能和解释;
def plot(self):
self.listo=[]
for val in self.lili:
# self.lili is a list that has all of the checkboxes value in it. Unchecked box values is 0 for all.Checked box values is changing based on month.
val=val.get()
if val !=0:# I created a empty list and add all the checked values in it.
self.listo.append(val)
else:
pass
self.tryn=len(self.listo)# I get the lenght of checked checkboxes.
query="WHERE "+"nameofmonth=(%s) OR "*self.tryn
#Creating a string variable for my query condition and multiplying it with lenght of the check box values.
#For example if 3 boxes checked by user we get something like that "WHERE nameofmonth=(%s) OR nameofmonth=(%s) OR nameofmonth=(%s) OR "
query=query[:-3]
#Getting rid of the extra OR and space at the end.
query2="WHERE "+"nameofmonth=(%s) OR "*self.tryn
#Couldnt use one query for 2 execute function so i just created another one and it worked xd
query2=query2[:-3]
query=("SELECT SUM(current_sales) FROM targets {}".format(query))
#Using this query i get this output (for 3 checked checkboxes)
#("SELECT SUM(current_sales) FROM targets WHERE nameofmonth=(%s) OR nameofmonth=(%s) OR nameofmonth=(%s))
self.cursor.execute(query,self.listo)
#Executing the query by using the list that has the values from checkboxes where values are not equal to zero.
self.current_sales1=self.cursor.fetchall()[0][0]
#Getting the SUM of the column i want in INT type.
推荐阅读
- python - 努力在 Python 中单击带有 selenium 的“确定”按钮
- python - 比较训练集中可训练参数和信息的数量
- android - Android系统UI状态栏如何显示秒数?
- neovim - 为什么我的 vim-airlines 的缓冲区没有主题样式?
- scala - sun.security.provider.certpath.SunCertPathBuilderException 将 Akka HTTP 更新到 10.1.14 到 10.2.4
- java - WebView 有时使用 URL 不显示 PDF 内容
- memory - 来自地址的 QEMU KVM 转储格式字符串
- python - 当主脚本也使用 argparse 时,寻找一种方法来显示导入脚本的参数
- python - 默认的 __iter__ 函数是什么?
- python - Flask 成功返回后返回 500 内部错误