我尝试为我的项目编写函数,该函数允许用户部分搜索并使用sqlite3语句根据“名称”列突出显示行/记录。但是我不知道如何使用它与SQLite3语句一起使用的功能,并匹配树视图中的行/记录。
def saerch_medicine():
#query with sqlite3
conn = sqlite3.connect("pharmacy.db")
cur = conn.cursor()
conn.commit()
cur.execute("SELECT name FROM medicine WHERE name LIKE '%"name_column"%'")
selections=[]
for i in cur:
tree. selections[0]
conn.close()
bt6 = Button(root,text="Serach according column name",width=10,command=saerch)
bt6.grid(row=11,column=1,pady=10,padx=20)
e6 = Entry(root,width=20)
e6.grid(row=11,column=3,pady=10,padx=20)
答案 0 :(得分:1)
假设tree
的第一列是name
列,然后修改saerch_medicine()
(saerch
应该是search
吗?),如下所示:< / p>
def saerch_medicine():
srch_name = e6.get().strip()
if srch_name:
conn = sqlite3.connect("pharmacy.db")
cur = conn.cursor()
cur.execute('SELECT name FROM medicine WHERE name LIKE "%{}%"'.format(srch_name))
matched = [rec[0] for rec in cur]
conn.close()
items = [row for row in tree.get_children() if tree.item(row, 'values')[0] in matched]
tree.selection_set(items)
但是,如果所有记录都已插入tree
,我想您可以直接搜索tree
:
def saerch_medicine():
srch_name = e6.get().strip()
if srch_name:
rows = [row for row in tree.get_children() if srch_name in tree.item(row, 'values')[0]]
tree.selection_set(rows)