-1

iv started using database for my program instead of .txt files and i would like to make a search function

iv got the basics of the search function ( dropdown menu with all the possible choices) but with most of the items theres multiple items in 1 column/row for example

column "food_name" " Ingredients" " recipe"

           potato casserole    potato,cheese,ect

but in my search function potato,cheese is 1 item how can i seprate it??

and my second question obviously theres going to be foods with similier ingredients how can i stop it from showing multiples of the same ingredient and still bring up the result of all the foods with the same ingredient??

option 1 has cheese selected option 2 has potato and cheese selected ( but i want it to be seprated) option 3 at the top it shows cheese and at the buttom it shows cheese (from 2 differnt foods) and i want to only have 1 instance of that ingredient but return all the foods with that ingredient

from tkinter import *
from tkinter.ttk import Combobox
import sqlite3

con = sqlite3.connect("Food.db")
c = con.cursor()

c.execute("SELECT Ingredients FROM Dinner_Order")
order = c.fetchall()
c.execute("SELECT Ingredients FROM Dinner_Stove")
stove = c.fetchall()
c.execute("SELECT Ingredients FROM Dinner_Oven")
oven = c.fetchall()
c.execute("SELECT Ingredients FROM Dinner_Cold")
cold = c.fetchall()
c.execute("SELECT Ingredients FROM Dinner_Simple")
simple = c.fetchall()
con.commit()

dinner_order = order
dinner_stove = stove
dinner_oven = oven
dinner_cold = cold
dinner_simple = simple

ws = Tk()
ws.title("Python Guides")
ws.geometry("200x200")



item_names = dinner_order + dinner_stove + dinner_oven + dinner_simple + dinner_cold

combo = Combobox(ws, state='readonly')
combo['values'] = item_names
combo.pack()

combo = Combobox(ws, state='readonly')
combo['values'] = item_names
combo.pack()

combo = Combobox(ws, state='readonly')
combo['values'] = item_names
combo.pack()

combo = Combobox(ws, state='readonly')
combo['values'] = item_names
combo.pack()

button = Button(ws, text="search", )#command=search_items)
button.pack()

ws.mainloop()
ZFV6
  • 1
  • 2

1 Answers1

0

I would suggest to split the result on ,, for example if the query returns "potato,cheese,ect" you would do:

"potato,cheese,ect".split(",")

Something like this:

>>> c.execute("SELECT Ingredients FROM Dinner_Simple WHERE food_name ='potato casserole'")
>>> ingredients = c.fetchall()
>>> print(ingredients)
"potato,cheese,ect"
>>> ingredients = ingredients.split(",")
>>> print(ingredients)
["potato", "cheese", "ect"]
ali jafargholi
  • 899
  • 8
  • 6
  • how do i .split(",") inside the database? ( i used DB browser(SQlite) to add all the infomation into the database) – ZFV6 Dec 29 '21 at 14:16
  • @ZFV6, what I meant was when you executed your SQL query and retrieved your data from your database, then you can do a simple `split` to convert that string to a list. – ali jafargholi Dec 29 '21 at 16:24
  • do i need to use the "where" code? because every food will have multiple ingredients and going through 40-50 differnt foods for each ingredient kinda beats the point of the database ( plus the user has the option to add more foods/ingredients/recipes and those wont apear in the search dropbox) and my other question is: is it possible to remove duplicates of the same ingredient? – ZFV6 Dec 29 '21 at 16:46
  • i have to convert it into string first before i can split it and then the option with potato,cheese becomes ('potato and \n\rcheese how can i remove the symbols? – ZFV6 Dec 29 '21 at 16:58
  • no you don't need to use `where`, that was just an example. to remove duplicates from a list you can convert it to a `set`, here is more info https://stackoverflow.com/questions/15768757/how-to-construct-a-set-out-of-list-items-in-python the best way to remove the character such as `\n` or `\r` is using regex, or you can also use `replace` method on your string – ali jafargholi Dec 30 '21 at 15:52