[英]SQlite Query using WHERE clause is not giving me the right output when I use a Tkinter drop down box
I'm trying to create a button that can filter students by their levels.我正在尝试创建一个按钮,可以按级别过滤学生。 For some reason, SQlite is not querying the data I want, instead, it gives me an empty list ([]).出于某种原因,SQlite 没有查询我想要的数据,而是给了我一个空列表 ([])。 When I don't use a drop down box and simply use an Entry box, it works fine.当我不使用下拉框而只使用输入框时,它工作正常。
#dropdown
levels = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
drop = StringVar()
drop.set(levels[0])
def levelsfunc(event):
levelsinfo = clickedsub2.get()
dropdownbox = OptionMenu(root, drop, *levels, command=levelsfunc)
#search function
def filterstudents():
query = "SELECT studentname, level FROM studenttable WHERE level = '%"+dropdown.get()+"%'"
c.execute(query)
filtered = c.fetchall()
print(filtered)
The %
characters are wildcards and they are used when you also use the operator LIKE
: %
字符是通配符,当您还使用运算符LIKE
时使用它们:
query = "SELECT studentname, level FROM studenttable WHERE level LIKE '%" + dropdown.get() + "%'"
This query will return all the rows that contain in the column level
the value of the string dropdown.get()
.此查询将返回列level
中包含字符串dropdown.get()
值的所有行。
Or better use a ?
或者更好地使用?
placeholder:占位符:
query = "SELECT studentname, level FROM studenttable WHERE level LIKE '%' || ? || '%'"
c.execute(query, (dropdown.get(),))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.