[英]Is there any way to set number of variables in SQL query based on length of list?
我正在嘗試根據用戶的選擇從數據庫中獲取每月值並將它們可視化。 我有復選框,如下所示;
為了根據用戶的選擇獲取每月銷售數字,我從復選框中獲取值並創建一個僅包含不等於零的值的新列表。 之后,我編寫了一個 Select 查詢,其中特定列具有列表中的任何值。 但是用戶只能選擇 1 個值或 3 個值或其他任何值,我的 sql 查詢必須根據它進行更改。 如果用戶選擇 1 個值,如果他們選擇 5 個值,我必須在查詢中只使用 1 個“%s”,我必須使用其中的 5 個。 所以我創建了一堆 if function 條件是列表的長度。 有沒有辦法根據列表的長度來安排 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]
在和我剛剛使用這個變量格式化我的查詢。
您可以在此處查看完整的 function 和說明;
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.