Advertisement
teslariu

Untitled

Dec 18th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sqlite3
  5.  
  6. while True:
  7.    
  8.     print("""
  9.    \nAdministración de la base de datos 'productosPC'
  10.    *-----------------------------------------------*
  11.    |    Menú de opciones:                          |
  12.    |    1) Agregar un nuevo registro               |
  13.    |    2) Consultar registros                     |
  14.    |    3) Borrar registros                        |
  15.    |    4) Salir                                   |
  16.    *-----------------------------------------------*
  17.    """)
  18.     opcion = input("Ingrese una opcion: ")
  19.    
  20.     if opcion == '1':
  21.        
  22.         # validación de los datos
  23.         while True:
  24.             try:
  25.                 ID = int(input("Ingrese un ID: "))
  26.                 break
  27.             except ValueError:
  28.                 print("El ID debe ser un número entero")
  29.         while True:
  30.             nombre = input("Ingrese el nombre del producto: ")
  31.             if len(nombre.strip()) > 0:
  32.                 break
  33.             else:
  34.                 print("Debe ingresar un nombre válido con algún caracter no vacío")
  35.         while True:
  36.             try:
  37.                 precio = int(input("Ingrese un precio: "))
  38.                 break
  39.             except ValueError:
  40.                 print("El precio debe ser un número entero")
  41.                
  42.         # guardamos los datos en la base
  43.         conn = sqlite3.connect("productosPC.db")
  44.         cursor = conn.cursor()
  45.         cursor.execute("INSERT INTO productos VALUES (?,?,?)",(ID, nombre, precio))
  46.         conn.commit()
  47.         conn.close()
  48.         print("Se ha guardado el registro")
  49.    
  50.     elif opcion == '2':
  51.         conn = sqlite3.connect("productosPC.db")
  52.         cursor = conn.cursor()
  53.         cursor.execute("SELECT * FROM productos")
  54.         datos = cursor.fetchall()
  55.        
  56.         print("ID".rjust(10), "Nombre".rjust(10), "Precio".rjust(10))
  57.         print("\t"+"-"*24)
  58.         if len(datos):
  59.             for dato in datos:
  60.                 print()
  61.                 for elemento in dato:
  62.                     print(str(elemento).rjust(10), end=" ")
  63.         else:
  64.             print("Base de datos vacía")
  65.         conn.close()
  66.            
  67.    
  68.     elif opcion == '3':
  69.         while True:
  70.             try:
  71.                 id_a_borrar = int(input("Ingrese el ID del producto a borrar: "))
  72.                 break
  73.             except ValueError:
  74.                 print("El ID debe ser un número entero")
  75.         conn = sqlite3.connect("productosPC.db")
  76.         cursor = conn.cursor()
  77.         cursor.execute("DELETE FROM productos WHERE id = {}".format(id_a_borrar))
  78.         conn.commit()
  79.         conn.close()
  80.         print(f"Se ha borrado el registro con ID = {id_a_borrar}")
  81.    
  82.     elif opcion == '4':
  83.         print("Gracias por utilizar este programa....")
  84.         break
  85.    
  86.     else:
  87.         print("Opción incorrecta")
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement