简体   繁体   中英

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 ([]). 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 :

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() .

Or better use a ? placeholder:

query = "SELECT studentname, level FROM studenttable WHERE level LIKE '%' || ? || '%'"
c.execute(query, (dropdown.get(),))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM