-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathricerca_per_ingredienti.py
52 lines (45 loc) · 1.75 KB
/
ricerca_per_ingredienti.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import mysql.connector
# Connessione a un database
db = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="ricettario"
)
# Collegamento cursore
cursor = db.cursor()
# Dizionario delle traduzioni
translations = {
'en': {
'input_ingredients': "\nEnter the ingredients separated by commas: (type 'quit' or 1 to exit) ",
'recipes_found': "\nRecipes found:",
'no_recipes_found': "No recipes found."
},
'it': {
'input_ingredients': "\nInserisci gli ingredienti separati da virgola: (scrivi 'quit' o 1 per uscire) ",
'recipes_found': "\nRicette trovate:",
'no_recipes_found': "Nessuna ricetta trovata."
}
}
def ricerca_per_ingredienti(lang):
while True:
array_ingredienti = []
ingredienti = input(translations[lang]['input_ingredients']).split(",")
if ingredienti[0].lower() == "quit" or ingredienti[0] == "1":
break
# Rimuovi spazi bianchi e crea pattern di ricerca
for i in ingredienti:
array_ingredienti.append(f"%{i.strip()}%")
# Prepara la query
sql = "SELECT * FROM `ricettario` WHERE " + " AND ".join(["ingredienti LIKE %s"] * len(array_ingredienti))
# Esegui la query con i parametri
cursor.execute(sql, array_ingredienti)
risultati = cursor.fetchall()
if risultati:
print(translations[lang]['recipes_found'])
for record in risultati:
print(f"- Nome: {record[2]}")
print(f"- Tipo: {record[1]}")
print("================================")
else:
print(translations[lang]['no_recipes_found'])