Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 30, 2021 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/3df982500b47499c82c4651e2716cb46 to your computer and use it in GitHub Desktop.
Save parzibyte/3df982500b47499c82c4651e2716cb46 to your computer and use it in GitHub Desktop.
from bd import obtener_conexion
def insertar_juego(nombre, descripcion, precio):
conexion = obtener_conexion()
with conexion.cursor() as cursor:
cursor.execute("INSERT INTO juegos(nombre, descripcion, precio) VALUES (%s, %s, %s)",
(nombre, descripcion, precio))
conexion.commit()
conexion.close()
def obtener_juegos():
conexion = obtener_conexion()
juegos = []
with conexion.cursor() as cursor:
cursor.execute("SELECT id, nombre, descripcion, precio FROM juegos")
juegos = cursor.fetchall()
conexion.close()
return juegos
def eliminar_juego(id):
conexion = obtener_conexion()
with conexion.cursor() as cursor:
cursor.execute("DELETE FROM juegos WHERE id = %s", (id,))
conexion.commit()
conexion.close()
def obtener_juego_por_id(id):
conexion = obtener_conexion()
juego = None
with conexion.cursor() as cursor:
cursor.execute(
"SELECT id, nombre, descripcion, precio FROM juegos WHERE id = %s", (id,))
juego = cursor.fetchone()
conexion.close()
return juego
def actualizar_juego(nombre, descripcion, precio, id):
conexion = obtener_conexion()
with conexion.cursor() as cursor:
cursor.execute("UPDATE juegos SET nombre = %s, descripcion = %s, precio = %s WHERE id = %s",
(nombre, descripcion, precio, id))
conexion.commit()
conexion.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment