Advertisement
teslariu

Untitled

Nov 14th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. import sqlite3
  6.  
  7. while True:
  8.    
  9.     print("""
  10.    Administración de base de datos 'productos.db'
  11.    *--------------------------------------------*
  12.        Menu de opciones:
  13.        1) Agregar un nuevo registro
  14.        2) Consultar registros
  15.        3) Borrar registros
  16.        4) Salir
  17.    *--------------------------------------------*
  18.    """)
  19.     opcion = input("Ingrese una opcion: ")
  20.    
  21.     if opcion == "1":
  22.         while True:
  23.             try:
  24.                 ID = int(input("Ingrese un ID: "))
  25.                 break
  26.             except ValueError:
  27.                 print("Error de tipo de dato: ID debe ser nro. entero")
  28.         while True:
  29.             nombre = input("Ingrese el nombre del producto: ")
  30.             if len(nombre) > 0 and nombre[0] !=" ":
  31.                 break
  32.             else:
  33.                 print("No se ingresó un nombre o su primer caracter es vacío")
  34.         while True:
  35.             try:
  36.                 precio = int(input("Ingrese un precio: "))
  37.                 break
  38.             except ValueError:
  39.                 print("Error de tipo de dato: ID debe ser nro. entero")
  40.        
  41.         conn = sqlite3.connect("productos.db")
  42.         cursor = conn.cursor()
  43.         cursor.execute("INSERT INTO productos VALUES (?,?,?)",(ID, nombre, precio))
  44.         conn.commit()
  45.         conn.close()
  46.         print("Se ha guardado el registro")
  47.        
  48.     elif opcion == "2":
  49.         conn = sqlite3.connect("productos.db")
  50.         cursor = conn.cursor()
  51.         cursor.execute("SELECT * FROM productos")
  52.         datos = cursor.fetchall()
  53.         for dato in datos:
  54.             print(dato)
  55.         conn.close()
  56.    
  57.     elif opcion =="3":
  58.         while True:
  59.             try:
  60.                 registro = int(input("Ingrese el ID del producto a eliminar: "))
  61.                 break
  62.             except ValueError:
  63.                 print("Error de tipo de dato: ID debe ser nro. entero")
  64.        
  65.         conn = sqlite3.connect("productos.db")
  66.         cursor = conn.cursor()
  67.         cursor.execute("DELETE FROM productos WHERE id = {}".format(registro))
  68.         conn.commit()
  69.         conn.close()
  70.         print("Registro eliminado")
  71.        
  72.     elif opcion == "4":
  73.         print("Hasta luego...")
  74.         break
  75.    
  76.     else:
  77.         print("Opción incorrecta")
  78.        
  79.                
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement