Python Forum
Problem with bindnig for query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with bindnig for query
#1
I had an issue with the results of my database query displaying curly brackets at the beginning and end of the results when it populated a tkinter widget. I found that if I change the insert string from output.insert(0,each_result) to output.insert(0,each_result[0]) it resolved the issue but it has raised a new issue.

The widget populated with the data now displays properly without the {} brackets but I can no longer click an item in that list displayed and run the query for that item.

The call to query the database for a selection made from the OptionMenu:
button = tkinter.Button(window_2, text="Load Category", font=('Times 9 bold'), bg = "#F9F8D6", fg = "#9A0615", command=select)
button.pack
The query for the selection made from the OptionMenu:
def select():
    try:
        output.delete(0, END)
        selection = var.get()
        c.execute('SELECT Recipe FROM Recipes WHERE Dish_Type = :selection ORDER BY Recipe DESC', {"selection": selection})
        results = c.fetchall()
        for each_result in results:
            output.insert(0,each_result[0])
    except:
        messagebox.showerror("DataBase Error", "Failed to load category")
The results of this query works properly and I no longer have the curly brackets showing the beginning and end of each result in the list that is displayed.(adding the [0] in the insert line resolved that)

The binding for the displayed list to be clicked:
output = tkinter.Listbox(window_2, font=('Times 9'), height = 20, width=40, bd=0, bg = "#FFD599", fg = '#9A0615', selectmode=SINGLE)
output.pack()
output.place(x=210, y=200)
yscroll = tkinter.Scrollbar(command=output.yview, orient=tkinter.VERTICAL)
yscroll.place(x=450, y=200)
output.configure(yscrollcommand=yscroll.set)
output.bind('<ButtonRelease-1>', getRecipe)
The procedure getRecipe to query the item in the displayed list when clicked:

This is the getRecipe procedure run when an item is clicked:
try:
    dish_type_entry.delete(0,END)
    index = output.curselection()[0]
    selected = output.get(index)
    c.execute('SELECT Dish_Type FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]})
    dish_type_results = c.fetchone()
    dish_type_entry.insert(0,dish_type_results)

    recipe_entry.delete(0,END)
    index = output.curselection()[0]
    selected = output.get(index)
    c.execute('SELECT Recipe FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]})
    recipe_results = c.fetchone()
    recipe_entry.insert(0,recipe_results[0])

    serves_entry.delete(0,END)
    index = output.curselection()[0]
    selected = output.get(index)
    c.execute('SELECT Serves FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]})
    serves_results = c.fetchone()\serves_entry.insert(0,serves_results[0])

    cook_time_entry.delete(0,END)
    index = output.curselection()[0]
    selected = output.get(index)
    c.execute('SELECT Cook_Time FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]})
    cook_time_results = c.fetchone()
    cook_time_entry.insert(0,cook_time_results[0])

    index = output.curselection()[0]
    selected = output.get(index)
    c.execute('SELECT Ingredients FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]})
    ingredients_results = c.fetchone()
    ingredients.insert(1.0,ingredients_results[0])
    ingredients.config(wrap=WORD)

    index = output.curselection()[0]
    selected = output.get(index)
    c.execute('SELECT Instructions FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]})
    instructions_results = c.fetchone()
    instructions.insert(1.0, instructions_results[0])
    instructions.config(wrap=WORD)
except:
    messagebox.showerror("DataBase Error", "Failed to load category")
This no longer populates the widgets and draws the error messagebox. This started when I added the [0] to the insert line of the for the selection made from the OptionMenu. (the second snip of code above)

Any advice and/or a point to documents that can explain why this is happening and how I can make the items in the displayed list clickable again without having the show the curly brackets would be appreciated.
"Often stumped... But never defeated."
Reply


Messages In This Thread
Problem with bindnig for query - by DT2000 - Mar-08-2019, 03:21 AM
RE: Problem with bindnig for query - by Larz60+ - Mar-08-2019, 03:57 AM
RE: Problem with bindnig for query - by DT2000 - Mar-08-2019, 04:16 AM
RE: Problem with bindnig for query - by Larz60+ - Mar-08-2019, 10:03 AM
RE: Problem with bindnig for query - by DT2000 - Mar-08-2019, 01:51 PM
RE: Problem with bindnig for query - by Larz60+ - Mar-08-2019, 05:13 PM
RE: Problem with bindnig for query - by DT2000 - Mar-09-2019, 02:12 AM
RE: Problem with bindnig for query - by Larz60+ - Mar-09-2019, 09:22 AM
RE: Problem with bindnig for query - by DT2000 - Mar-11-2019, 07:07 PM
RE: Problem with bindnig for query - by DT2000 - Mar-15-2019, 11:06 PM
RE: Problem with bindnig for query - by DT2000 - Mar-18-2019, 02:55 AM
RE: Problem with bindnig for query - by Larz60+ - Mar-18-2019, 09:11 PM
RE: Problem with bindnig for query - by Larz60+ - Mar-19-2019, 02:16 AM
RE: Problem with bindnig for query - by DT2000 - Mar-19-2019, 08:50 AM
RE: Problem with bindnig for query - by DT2000 - Mar-20-2019, 04:46 PM
RE: Problem with bindnig for query - by Larz60+ - Mar-21-2019, 12:49 AM
RE: Problem with bindnig for query - by DT2000 - Mar-21-2019, 01:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem Using SQL Placeholder In MySQL Query AdeS 11 6,259 Jul-31-2021, 12:19 AM
Last Post: Pedroski55
  MySQLdb, problem with query with user-defined variables buran 6 6,470 Feb-03-2017, 06:16 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020