如何使用textBox Entry搜索数据库?

时间:2018-04-02 15:14:20

标签: python sql database python-3.x sqlite

如何使用textBox Entry搜索数据库?

如果我提供一个全名,我会有一些代码显示正确的结果,但我想在输入一个字母时向我显示包含该字母的所有结果。

from tkinter import *
import sqlite3

def printRecords():
    connection = sqlite3.connect('baza.db')
    cur = connection.cursor()
    cur.execute("select *  from Zaposleni where  Ime LIKE  ?", (z.get(),))
    connection.commit()
    variablename = cur.fetchall()
    print (variablename)

root = Tk()
root.title ( "Search a Book")

z= StringVar()

#Label
Label(root, text = "Enter the name of the book :      ").grid()
#Textbox
bookSearchEntry = Entry(root, textvariable = z)
bookSearchEntry.grid(row = 0, column = 1)
#Button
bookSearchButton = Button(root, text = "Search", command = printRecords)
bookSearchButton.grid(row = 0, column = 2)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。这是一个解决方案:

 cur.execute("select *  from Zaposleni where  Ime LIKE '%" + z.get() + "%'")