简体   繁体   English

当我使用 Tkinter 下拉框时,使用 WHERE 子句的 SQlite 查询没有给我正确的输出

[英]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.

相关问题 在sqlite3查询where子句的下拉菜单中如何在默认情况下选择所有值 - How to select all values in case of default in drop-down menu in sqlite3 query where clause 如何使用从我的SQLite数据库中检索的数据填充tkinter下拉框? - How do I populate tkinter drop-down box with data retrieved from my SQLite database? 为什么这个程序在我在 print() 函数中使用 + 时给我错误,并在我使用 ',' 时给我输出 - why does this program giving me the error when i am using + in print() function and giving me the output when i am using ',' 每次按下“刷新”时,如何用新数据填充 tkinter 下拉框? - How do i populate tkinter drop-down box with new data everytime i pressed “refresh”? Tkinter 在使用嵌套类时在输入框中显示 output - Tkinter displaying output in a entry box using when using nested classes 当我尝试运行它时,Tkinter 给了我一个 _tkinter.TclError: bad event type or keysym "button" - Tkinter is giving me a _tkinter.TclError: bad event type or keysym "button" when i try to run it 如何在我的tkinter组合框的下拉列表中添加值? - How do I add values in the drop down of my tkinter combo box? 在子句中使用右连接和WHERE - Using Right Join and WHERE IN Clause 当我尝试设置我的徽标时,Python tkinter 给我一个错误(GUI WINDOW) - Python tkinter giving me an error when I try to set me logo (GUI WINDOW) 为什么numpy.where给我这个输出? - Why is numpy.where giving me this output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM