Python Forum
SELECT statement query question using a variable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: SELECT statement query question using a variable (/thread-16326.html)



SELECT statement query question using a variable - DT2000 - Feb-23-2019

I am attempting to figure out how to use a variable from an MenuOption widget selection as the data in the specific column "Dish_Type" in my SQLite query.
The idea is to make a selection from the list in the MenuOption and that would be stored in the variable called "selection". During the query it should use the variable to query and print all the selected items in the column "Dish_Type" (such as Appetizers). I have not been successful in getting the query statement to successfully query and print the data for a selected category in the MenuOption yet.

Code:
    def select():
        try:
            selection = var.get()
            c.execute('SELECT * FROM Recipes WHERE Dish_Type = "+selection+" ')
            data = c.fetchall()
            print(data)
        except:
            messagebox.showerror("Error", "Data Base Error. Query failed")

    var = tkinter.StringVar(window_2)
    var.set('Menu')

    choices = [
        'Appetizers',
        'Beef',
        'Bread',
        'Cake',
        'Chicken',
        'Chilli',
        'Curry',
        'Desert',
        'Drinks',
        'Egg',
        'Fish',
        'Pasta',
        'Pork',
        'Potato',
        'Rice',
        'Salad',
        'Sandwich',
        'Sauce',
        'Sea Food',
        'Slow Cooker',
        'Soup',
        'Stew',
        'Tofu',
        'Vegetables']

    option = tkinter.OptionMenu(window_2, var, *choices)
    option.place(x=215, y=120)

    button = tkinter.Button(window_2, text="Load Category", command=select)
    button.pack
    button.place(x=363, y=120
I thought this "c.execute('SELECT * FROM Recipes WHERE Dish_Type = "+selection+"') would work by using the variable "selection" in the query but it is not pulling the data in the column with the name Appetizers yet. (if Appetizers was selected in the menu)


RE: SELECT statement query question using a variable - buran - Feb-23-2019

Did you even check the docs for sqlite3 module? There is clear example how to do that...


RE: SELECT statement query question using a variable - DT2000 - Feb-23-2019

buran, I greatly appreciate the link, it fixed it up quite easily. I had read a great deal from various docs on the issue but nothing was specific in resolving my problem and I did not see this doc but wish I had at the beginning.

I appreciate your assistance in providing the link for me to find the answer.

Cheer