users api done

This commit is contained in:
andrei-mihnea-cerbu
2024-01-09 03:06:03 +02:00
parent 6ece0f37b1
commit f80b8c38db
35 changed files with 221 additions and 11 deletions
@@ -0,0 +1,38 @@
import psycopg2
DATABASE_CONFIG = {
'dbname': 'messenger',
'user': 'andrei_cerbu',
'password': 'andrei',
'host': 'localhost',
'port': '5432'
}
def create_connection():
try:
connection = psycopg2.connect(**DATABASE_CONFIG)
return connection
except psycopg2.Error as e:
print(f"Error: Unable to connect to the database\n{e}")
return None
def execute_query(connection, query, params=None, fetchall=False):
try:
with connection.cursor() as cursor:
cursor.execute(query, params)
if fetchall:
result = cursor.fetchall()
return result
else:
connection.commit() # Commit the transaction for INSERT, UPDATE, DELETE queries
except psycopg2.Error as e:
print(f"Error: Unable to execute the query\n{e}")
return None
def close_connection(connection):
if connection:
connection.close()
print("Connection closed.")