有什么方法可以根据列表的长度在 SQL 查询中设置变量数?
Is there any way to set number of variables in SQL query based on length of list?
我正在尝试根据用户的选择从数据库中获取月度值并将其可视化。
我有复选框,如下所示;
Checkboxes I used
为了根据用户的选择获取月度销售数字,我从复选框中获取值并创建一个仅包含不等于零的值的新列表。之后我写了一个 Select 查询,其中特定列具有列表中的任何值。但是用户只能选择 1 个值或 3 个值或其他任何值,我的 sql 查询必须根据此更改。如果用户选择 1 个值,我必须在查询中仅使用 1 个“%s”,如果他们选择 5 个值,我必须使用其中的 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 和最后的 space;
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.
我正在尝试根据用户的选择从数据库中获取月度值并将其可视化。 我有复选框,如下所示;
Checkboxes I used
为了根据用户的选择获取月度销售数字,我从复选框中获取值并创建一个仅包含不等于零的值的新列表。之后我写了一个 Select 查询,其中特定列具有列表中的任何值。但是用户只能选择 1 个值或 3 个值或其他任何值,我的 sql 查询必须根据此更改。如果用户选择 1 个值,我必须在查询中仅使用 1 个“%s”,如果他们选择 5 个值,我必须使用其中的 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 和最后的 space;
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.