-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
32 lines (29 loc) · 988 Bytes
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import psycopg2, os
from dotenv import load_dotenv
load_dotenv()
def dbQuery(table):
connection = None
cursor = None
try:
connection = psycopg2.connect(
host=os.getenv("DB_HOST"),
database=os.getenv("DB_DB"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASS")
)
cursor = connection.cursor()
# print("Connected to the database")
try:
cursor.execute(f"SELECT * FROM {table}")
records = cursor.fetchall()
except (Exception, psycopg2.Error) as error:
print(f"Error executing the query: {error}")
finally:
if cursor is not None:
cursor.close()
if connection is not None:
connection.close()
print("Database connection closed")
except (Exception, psycopg2.Error) as error:
print(f"Error while connecting to the database: {error}")
return records