-
-
Save parzibyte/54d5f708bc144814ad02fe8cda6e8b91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 = "SELECT id, nombre, edad FROM mascotas WHERE nombre like %s" | |
# Para Maggie | |
busqueda = "agg" | |
cursor.execute(consulta, ("%" + busqueda + "%",)) | |
# Con fetchall traemos todas las filas | |
mascotas = cursor.fetchall() | |
# Recorrer e imprimir | |
for mascota in mascotas: | |
print(mascota) | |
except psycopg2.Error as e: | |
print("Ocurrió un error: ", e) | |
finally: | |
conexion.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment