|
| 1 | +from os import system |
| 2 | +import time |
| 3 | +import conexion as conn |
| 4 | +db = conn.DB() |
| 5 | +system("clear") |
| 6 | + |
| 7 | +def create(): |
| 8 | + name = str(input("INGRESA SU NOMBRE: ")) |
| 9 | + email = str(input("INGRESA SU EMAIL: ")) |
| 10 | + if(len(name) > 0 and len(email) > 0): |
| 11 | + sql = "INSERT INTO sistema(name,email) VALUES(?,?)" |
| 12 | + parametros = (name,email) |
| 13 | + db.ejecutar_consulta(sql,parametros) |
| 14 | + print("Insertados") |
| 15 | + |
| 16 | +def read(): |
| 17 | + sql = "SELECT * FROM sistema" |
| 18 | + result = db.ejecutar_consulta(sql) |
| 19 | + for data in result: |
| 20 | + print(""" |
| 21 | + ID : {} |
| 22 | + NOMBRE : {} |
| 23 | + EMAIL : {} |
| 24 | + """.format(data[0],data[1],data[2])) |
| 25 | + |
| 26 | +def update(): |
| 27 | + id = int(input("INGRESA EL ID: ")) |
| 28 | + if(id != 0): |
| 29 | + name = str(input("INGRESA SU NOMBRE: ")) |
| 30 | + email = str(input("INGRESA SU EMAIL: ")) |
| 31 | + if(len(name) > 0 and len(email) > 0): |
| 32 | + sql = "UPDATE sistema SET name=?,email=? WHERE id=?" |
| 33 | + parametros = (name,email,id) |
| 34 | + db.ejecutar_consulta(sql,parametros) |
| 35 | + print("Actualizado!") |
| 36 | + else: |
| 37 | + print("Se require un ID") |
| 38 | + |
| 39 | +def delete(): |
| 40 | + id = int(input("INGRESA EL ID: ")) |
| 41 | + if(id != 0): |
| 42 | + sql = "DELETE FROM sistema WHERE id=?" |
| 43 | + parametros = (id,) |
| 44 | + db.ejecutar_consulta(sql,parametros) |
| 45 | + print("Eliminado!") |
| 46 | + else: |
| 47 | + print("Se require un ID") |
| 48 | + |
| 49 | +def search(): |
| 50 | + nombre = str(input("Buscar por nombre: ")) |
| 51 | + if(len(nombre) > 0): |
| 52 | + sql = "SELECT * FROM sistema WHERE name LIKE ?" |
| 53 | + parametros = ('%{}%'.format(nombre),) |
| 54 | + result = db.ejecutar_consulta(sql,parametros) |
| 55 | + for data in result: |
| 56 | + print(""" |
| 57 | + +ID : {} |
| 58 | + +NOMBRE : {} |
| 59 | + +EMAIL : {}""".format(data[0],data[1],data[2])) |
| 60 | +while True: |
| 61 | + print("=========================================") |
| 62 | + print("\tCRUD CON SQLite3") |
| 63 | + print("=========================================") |
| 64 | + print("\t[1] Insertar registro") |
| 65 | + print("\t[2] Listar registros") |
| 66 | + print("\t[3] Actualizar registros") |
| 67 | + print("\t[4] Eliminar registros") |
| 68 | + print("\t[5] Buscar registros") |
| 69 | + print("\t[6] Salir") |
| 70 | + print("=========================================") |
| 71 | + |
| 72 | + try: |
| 73 | + opcion = int(input("Selecciona una opcion: ")) |
| 74 | + if(opcion == 1): |
| 75 | + create() |
| 76 | + time.sleep(1) |
| 77 | + system("clear") |
| 78 | + elif (opcion == 2): |
| 79 | + read() |
| 80 | + time.sleep(1) |
| 81 | + elif (opcion == 3): |
| 82 | + update() |
| 83 | + time.sleep(1) |
| 84 | + system("clear") |
| 85 | + elif (opcion == 4): |
| 86 | + delete() |
| 87 | + time.sleep(1) |
| 88 | + system("clear") |
| 89 | + elif (opcion == 5): |
| 90 | + search() |
| 91 | + |
| 92 | + elif (opcion == 6): |
| 93 | + break |
| 94 | + except: |
| 95 | + print("Por favor, selecciona las opciones correctas") |
| 96 | + system("clear") |
0 commit comments