mysql.connector.errors.ProgrammingError:1054(42S22):where子句中的未知列“PY_VAR0”

vngu2lb8  于 12个月前  发布在  Mysql
关注(0)|答案(2)|浏览(107)

我试图使一个tkinter应用程序(真实的的快速),实际上从我的数据库中提取搜索结果,但是,我希望用户指定列名,所以我使用下拉菜单,然后他们可以从下拉菜单中选择,但当我在数据库命令中使用该变量时,它返回错误mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'PY_VAR0' in 'where clause'。提前感谢:)
代码:

from tkinter import *
import mysql.connector as mysql
import datetime

root = Tk()

def db():
    con = mysql.connect(host='localhost', user='root', password='*****', database='BOOK')
    c = con.cursor()
    c.execute(f"SELECT * from lessc where `{g}` like '{e1.get()}%';")
    
    rec = c.fetchall()
    
    print_record = ""
    for recs in rec:
        print_record += 'BOOK: ' + str(recs[1]) + '\t' + ' Author: ' + str(recs[2]) + "\n"
    
    query_label = Label(root,text=print_record)
    query_label.pack()
    c.execute('commit')
    con.close()
    e1.delete(0,END)

l1 = Label(root,text='Book name')
l1.pack()

e1 = Entry(root)
e1.pack()

b1 = Button(root,text='Search',command=db)
b1.pack()

b2 = Button(root,text='Exit',command=root.destroy)
b2.pack()

gen = ['Sl.no.','Title of the book','Authors','Editors','Revised by','Edition','Accession no.','Call no.','Item','Subject','Year','ISBN','Publisher','Updated on','Price','Supplier']
g = StringVar()
g.set('Choose Search method')
opt_g = OptionMenu(root,g,*gen)
opt_g.pack()

root.mainloop()
pn9klfpd

pn9klfpd1#

c.execute("SELECT * from lessc where `Title of the book` like %s", (f'{e1.get()}%',))

而不是用于正确引用和参数替换。

t3irkdon

t3irkdon2#

答案很简单,只要使用(在我的情况下)g.get()而不是我单独使用g,这是编码时的错误。Thanks guys:)

相关问题