-
-
Save parzibyte/1358d65172d489f3b2bd4d8ea0c3c677 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 | |
import json | |
# Leer las credenciales de un archivo JSON | |
# Recomendado: https://parzibyte.me/blog/2019/06/09/json-python-codificar-decodificar/ | |
with open("credenciales.json") as archivo_credenciales: | |
credenciales = json.load(archivo_credenciales) | |
# Como la conexión devuelve un diccionario podemos convertirlo fácilmente | |
# a "kwargs" o key arguments ;) | |
# Recomendado: https://parzibyte.me/blog/2018/12/20/args-kwargs-python/ | |
try: | |
conexion = psycopg2.connect(**credenciales) | |
except psycopg2.Error as e: | |
print("Ocurrió un error al conectar a PostgreSQL: ", e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment