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
#2
Did you even check the docs for sqlite3 module? There is clear example how to do that...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
"Often stumped... But never defeated."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question of using not in if statement and .. akbarza 3 362 Feb-29-2024, 08:40 AM
Last Post: Gribouillis
  Quick question/help regarding my variable Extra 5 1,490 May-06-2022, 12:01 AM
Last Post: Extra
  Error using mariadb select query with form in python? shams 2 1,956 Jul-29-2021, 12:30 PM
Last Post: shams
  An IF statement with a List variable dedesssse 3 7,949 Jul-08-2021, 05:58 PM
Last Post: perfringo
  define a variable using an if statement Margaridalopes 2 2,146 Oct-24-2020, 05:47 PM
Last Post: jefsummers
  Yield statement question DPaul 6 2,448 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,611 Sep-20-2019, 03:49 PM
Last Post: Yoriz
  Python and Postgresql syntax select statement Nesreenmhd 1 4,938 Sep-07-2019, 06:08 PM
Last Post: ndc85430
  SQL query with a variable amount of parameters Antares 10 6,743 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,413 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