Commit inicial
This commit is contained in:
35
lib/log.py
Normal file
35
lib/log.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import psycopg2
|
||||
from psycopg2.extras import DictCursor
|
||||
|
||||
def connect_to_database(dbname="sgi", user="postgres", password="sys4lci", host="200.13.89.27", port="5432"):
|
||||
"""
|
||||
Establece una conexión a la base de datos y la retorna.
|
||||
"""
|
||||
connection = psycopg2.connect(
|
||||
dbname=dbname,
|
||||
user=user,
|
||||
password=password,
|
||||
host=host,
|
||||
port=port
|
||||
)
|
||||
return connection
|
||||
|
||||
|
||||
def query_all(sql):
|
||||
with connect_to_database() as conn:
|
||||
with conn.cursor(cursor_factory=DictCursor) as cur:
|
||||
cur.execute(sql)
|
||||
return cur.fetchall()
|
||||
|
||||
def query_single(sql):
|
||||
with connect_to_database() as conn:
|
||||
with conn.cursor(cursor_factory=DictCursor) as cur:
|
||||
cur.execute(sql)
|
||||
return cur.fetchone() # Returns a dictionary-like object
|
||||
|
||||
def execute_query(sql):
|
||||
with connect_to_database() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql)
|
||||
conn.commit() # Commit to save the insert operation
|
||||
|
||||
Reference in New Issue
Block a user