有没有办法根据列表的长度设置SQL查询中的变量数



我正试图根据用户的选择从数据库中获取每月的值,并将其可视化。我有复选框,你可以在下面看到;

复选框我使用

为了根据用户的选择获得每月的销售数字,我从复选框中获取值,并创建一个只包含不等于零的值的新列表。之后,我编写了一个Select查询,其中一个特定列具有列表中的任何值。但是用户只能选择1个值或3个值或其他值,我的sql查询必须根据这一点进行更改。如果用户选择1个值,则我必须仅使用1〃%s";在我的查询中,如果他们选择了5个值,我必须使用其中的5个。所以我创建了一组if函数,其中condition是列表的长度。有没有办法根据列表的长度来排列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.

最新更新