从tkinter输入框中搜索记录时如何从sqlite数据库返回记录(Python)

时间:2016-11-11 10:38:28

标签: python mysql sqlite tkinter

我正在使用数据库为库创建程序,以存储有关书籍的详细信息,并将tkinter作为gui。该程序的一个功能是用户可以输入书籍的名称,它将搜索数据库并返回包含该名称的书籍的记录。以下是我目前对此功能的代码:

def bookSearch(event):
    top = Toplevel()
    top.title("Book Search")

    Label(top, text = "Enter the name of the book you are searching for: ").grid()

    bookSearchEntry = Entry(top)
    bookSearchEntry.grid(row = 0, column = 1)


    def dbSearchForBooks(event): 
        getRecord = c.execute("Select * FROM bookList WHERE BookName = ?",(bookSearchEntry.get(),))
        strGetRecord = str(getRecord)

        t = Text(top, height = 100, width = 100) #creates a text widget where the records will be inserted into 
        t.grid(row = 1, column = 0, columnspan = 3) #packs the widget into the window
        t.insert(END, strGetRecord + "\n") #inserts the records into the window

    bookSearchButton = Button(top, text = "Search", command = dbSearchForBooks(event))
    bookSearchButton.grid(row = 0, column = 2)

我没有收到此代码的任何错误消息,但是,它只是说 <sqlite3.Cursor object at 0x0406B9E0> 在文本小部件中。首次运行该功能时它应为空,然后输入我正在搜索的书的名称,例如“哈利波特”它应该用bookList表中的行更新文本小部件,其中“哈利波特”是书名。我搜索时文本小部件不会更新,它只是不断地说

<sqlite3.Cursor object at 0x0406B9E0>

我很难解决这个问题,所以感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

猜猜您需要从此容器中获取记录(就像您使用列表一样),例如strGetRecord = ' '.join([str(record) for record in getRecord])