Python Forum
SELECT statement query question using a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SELECT statement query question using a variable
#1
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)
"Often stumped... But never defeated."
Reply


Messages In This Thread
SELECT statement query question using a variable - by DT2000 - Feb-23-2019, 06:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  question of using not in if statement and .. akbarza 3 449 Feb-29-2024, 08:40 AM
Last Post: Gribouillis
  Quick question/help regarding my variable Extra 5 1,551 May-06-2022, 12:01 AM
Last Post: Extra
  Error using mariadb select query with form in python? shams 2 2,024 Jul-29-2021, 12:30 PM
Last Post: shams
  An IF statement with a List variable dedesssse 3 8,308 Jul-08-2021, 05:58 PM
Last Post: perfringo
  define a variable using an if statement Margaridalopes 2 2,192 Oct-24-2020, 05:47 PM
Last Post: jefsummers
  Yield statement question DPaul 6 2,528 Sep-26-2020, 05:18 PM
Last Post: DPaul
  I need a query to select the numbers that include 2 digits of 3 omidvakili 1 1,652 Sep-20-2019, 03:49 PM
Last Post: Yoriz
  Python and Postgresql syntax select statement Nesreenmhd 1 5,004 Sep-07-2019, 06:08 PM
Last Post: ndc85430
  SQL query with a variable amount of parameters Antares 10 6,888 Jul-08-2019, 02:24 PM
Last Post: Antares
  How to store the value from variable into list & run a statement then put in variable searching1 1 2,470 May-29-2019, 06:36 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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