Skip to content

Commit

Permalink
CRUD CONSOLE
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-emarquez committed Aug 7, 2021
1 parent 026fb87 commit 3c9e5bf
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"python.pythonPath": "C:\\Users\\brian\\AppData\\Local\\Programs\\Python\\Python39\\python.exe"
"python.pythonPath": "/bin/python3"
}
Binary file added Crud Console/__pycache__/conexion.cpython-38.pyc
Binary file not shown.
11 changes: 11 additions & 0 deletions Crud Console/conexion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sqlite3
#Import database
database = "database.db"
class DB:
def ejecutar_consulta(self,consulta,parametros = ()):
with sqlite3.connect(database) as conn:
self.cursor = conn.cursor()
result = self.cursor.execute(consulta,parametros)
conn.commit()
return result

Binary file added Crud Console/database.db
Binary file not shown.
96 changes: 96 additions & 0 deletions Crud Console/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from os import system
import time
import conexion as conn
db = conn.DB()
system("clear")

def create():
name = str(input("INGRESA SU NOMBRE: "))
email = str(input("INGRESA SU EMAIL: "))
if(len(name) > 0 and len(email) > 0):
sql = "INSERT INTO sistema(name,email) VALUES(?,?)"
parametros = (name,email)
db.ejecutar_consulta(sql,parametros)
print("Insertados")

def read():
sql = "SELECT * FROM sistema"
result = db.ejecutar_consulta(sql)
for data in result:
print("""
ID : {}
NOMBRE : {}
EMAIL : {}
""".format(data[0],data[1],data[2]))

def update():
id = int(input("INGRESA EL ID: "))
if(id != 0):
name = str(input("INGRESA SU NOMBRE: "))
email = str(input("INGRESA SU EMAIL: "))
if(len(name) > 0 and len(email) > 0):
sql = "UPDATE sistema SET name=?,email=? WHERE id=?"
parametros = (name,email,id)
db.ejecutar_consulta(sql,parametros)
print("Actualizado!")
else:
print("Se require un ID")

def delete():
id = int(input("INGRESA EL ID: "))
if(id != 0):
sql = "DELETE FROM sistema WHERE id=?"
parametros = (id,)
db.ejecutar_consulta(sql,parametros)
print("Eliminado!")
else:
print("Se require un ID")

def search():
nombre = str(input("Buscar por nombre: "))
if(len(nombre) > 0):
sql = "SELECT * FROM sistema WHERE name LIKE ?"
parametros = ('%{}%'.format(nombre),)
result = db.ejecutar_consulta(sql,parametros)
for data in result:
print("""
+ID : {}
+NOMBRE : {}
+EMAIL : {}""".format(data[0],data[1],data[2]))
while True:
print("=========================================")
print("\tCRUD CON SQLite3")
print("=========================================")
print("\t[1] Insertar registro")
print("\t[2] Listar registros")
print("\t[3] Actualizar registros")
print("\t[4] Eliminar registros")
print("\t[5] Buscar registros")
print("\t[6] Salir")
print("=========================================")

try:
opcion = int(input("Selecciona una opcion: "))
if(opcion == 1):
create()
time.sleep(1)
system("clear")
elif (opcion == 2):
read()
time.sleep(1)
elif (opcion == 3):
update()
time.sleep(1)
system("clear")
elif (opcion == 4):
delete()
time.sleep(1)
system("clear")
elif (opcion == 5):
search()

elif (opcion == 6):
break
except:
print("Por favor, selecciona las opciones correctas")
system("clear")

0 comments on commit 3c9e5bf

Please sign in to comment.