Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created June 18, 2019 01:49
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/e08fdfb9a948c9f5a4f2e74aebe1b034 to your computer and use it in GitHub Desktop.
Save parzibyte/e08fdfb9a948c9f5a4f2e74aebe1b034 to your computer and use it in GitHub Desktop.
"""
Conexión a PostgreSQL con Python
Ejemplo de CRUD evitando inyecciones SQL
@author parzibyte
Más tutoriales en:
parzibyte.me/blog
"""
import psycopg2
from bd import conexion
try:
with conexion.cursor() as cursor:
consulta = "DELETE FROM mascotas WHERE edad < %s;"
# También podría ser sin where
#consulta = "DELETE FROM mascotas"
edad = 2
cursor.execute(consulta, (edad,))
# No olvidemos hacer commit cuando hacemos un cambio a la BD
conexion.commit()
except psycopg2.Error as e:
print("Error eliminando: ", e)
finally:
conexion.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment