Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- import sqlite3
- def mostrar_menu():
- return """
- Menu de administracion del restaurante
- *-------------------------------------------*
- 1. Agregar categoria
- 2. Agregar plato
- 3. Mostrar carta
- 4. Salir
- *-------------------------------------------*
- """
- def agregar_categoria(nombre):
- try:
- conn.execute("INSERT INTO categoria VALUES (null, ?)", (nombre,))
- except sqlite3.IntegrityError:
- print(f"Error, ya existe la categoria {nombre}")
- else:
- conn.commit()
- def agregar_plato():
- try:
- cursor.execute("SELECT * FROM categoria ORDER BY id")
- except sqlite3.OperationalError:
- print(f"No se pudieron cargar categorias")
- else:
- categorias = cursor.fetchall()
- if categorias:
- id_categoria = []
- print("Categorias")
- print("==========")
- print("ID | nombre")
- print("-------------")
- for ID,nombre in categorias:
- id_categoria.append(ID)
- print(f"{ID} | {nombre}")
- while True:
- try:
- ID = int(input("\nSeleccione un id: "))
- except ValueError:
- print("Debe elegir un nro entero")
- else:
- if ID in id_categoria:
- break
- print(f"ID {ID} inexistente")
- nombre_plato = input("Ingrese el nombre del plato: ")
- try:
- conn.execute("INSERT INTO platos VALUES (null, ?,?)", (nombre_plato,ID))
- except sqlite3.IntegrityError:
- print(f"Error, ya existe el plato {nombre_plato}")
- else:
- conn.commit()
- else:
- print("No hay categorias de platos")
- def mostrar_carta():
- query = "SELECT categoria.id, categoria.nombre, platos.id, platos.nombre FROM categoria\
- LEFT JOIN platos\
- ON platos.id_categoria = categoria.id\
- ORDER BY categoria.id, platos.id"
- cursor.execute(query)
- platos = cursor.fetchall()
- if platos:
- print("\nID categoria ID plato ")
- print("-----------------------------")
- for plato in platos:
- print()
- for elemento in plato:
- if isinstance(elemento,str):
- print("{:<10}".format(elemento), end=" ")
- if isinstance(elemento,int):
- print("{:^3}".format(elemento), end=" ")
- print()
- else:
- print("No hay ningún plato en la carta")
- while True:
- conn = sqlite3.connect("restaurante.sqlite")
- cursor = conn.cursor()
- print(mostrar_menu())
- opcion = input("Seleccione una opción: ")
- if opcion == "1":
- agregar_categoria(input("Ingrese el nombre: "))
- elif opcion == "2":
- agregar_plato()
- elif opcion == "3":
- mostrar_carta()
- elif opcion == "4":
- conn.close()
- print("Hasta luego...")
- break
- else:
- print("Opción incorrecta")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement