0

I am making a program for a library using a database to store details about books, with tkinter as a gui. A feature of the program is where the user can enter the name of a book and it will search the database and return records with books containing that name. Here is the code I currently have for this feature:

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)

I am not getting any error messages with this code, however, it is just saying <sqlite3.Cursor object at 0x0406B9E0> in the text widget. It should be empty when the function is first run, then I enter the name of the book I am searching for e.g. "Harry Potter" and it should update the text widget with the row in the bookList table for the row where "Harry Potter" is the book name. The text widget does not update when I search, it is just constantly saying

<sqlite3.Cursor object at 0x0406B9E0>

I am stumped on how to fix this so any help is appreciated, thank you.

9
  • you don't want to create a text widget every time you do a search. Create the text widget once, and then you can replace the old search with the new. As written you're just stacking one text widget on top of another. Commented Nov 11, 2016 at 12:20
  • Done that and it's still acting the same, the widget isn't being updated Commented Nov 11, 2016 at 13:53
  • That won't solve the updating problem, that simply solves the memory leak you have of creating widget on top of widget on top of widget. Commented Nov 11, 2016 at 13:58
  • Oh right. I've just done a bit of testing and i've found that the dbSearchForBooks function is running straight away. I only want the function to run when the "Search" button is pressed. As the function is running straight away it doesn't give time to enter anything into the entry box so the query is returning nothing as the entry box is empty. Surely there's a way around this? Commented Nov 11, 2016 at 14:03
  • bookSearchButton=Button(..., command=dbSearchForBooks) Commented Nov 11, 2016 at 14:05

1 Answer 1

1

Guess you need to fetch record from this container (like you do it with lists) e.g. strGetRecord = ' '.join([str(record) for record in getRecord])

Sign up to request clarification or add additional context in comments.

3 Comments

also do not forget that there could be also not only one record executed. so for taking records from the first found record you should use getRecord[0]
I've used that code but now the text widget just shows this: ('', '') When I press the search button the text widget doesn't update, it's just staying the same
Are the two empty stings the columns in table? Because the bookList table where the query is getting the results from has two columns. But why would the fields be empty in the program?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.