forked from nfoster1492/ClassMateBot-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
36 lines (31 loc) · 786 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
36
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="allow")
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)
CONN.rollback()
raise e
if cur.description is None:
rows = []
else:
rows = cur.fetchall()
if not TESTING_MODE:
CONN.commit()
cur.close()
return rows