forked from SE21-Team2/ClassMateBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
35 lines (30 loc) · 770 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
32
33
34
35
import os
import psycopg2
from dotenv import load_dotenv
load_dotenv()
CONN = None
TESTING_MODE = False
# def connect():
# global CONN
DATABASE_URL = os.getenv('DATABASE_URL')
try:
CONN = psycopg2.connect(DATABASE_URL, sslmode='require')
print('PostgreSQL connection successful')
except (Exception, psycopg2.DatabaseError) as error:
print(error)
def query(sql, args=()):
''' query the database and get back rows selected/modified '''
cur = CONN.cursor()
try:
cur.execute(sql, args)
except Exception as e:
print(type(e).__name__, e)
raise e
if cur.description is None:
rows = []
else:
rows = cur.fetchall()
if not TESTING_MODE:
CONN.commit()
cur.close()
return rows