-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
40 lines (31 loc) · 1.01 KB
/
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
37
38
39
40
import psycopg2, os
import psycopg2.extras
class Db():
def __init__(self):
self._conn_str = os.environ.get('DATABASE_URL')
def query(self, sql, data=None, verbose=False):
conn = psycopg2.connect(self._conn_str)
cur = conn.cursor(cursor_factory = psycopg2.extras.DictCursor)
if verbose == True:
print(cur.mogrify(sql, data))
cur.execute(sql, data)
conn.commit()
if cur.description is not None:
results = cur.fetchall()
cur.close()
conn.close()
if cur.description is not None:
if len(results) == 1:
return results[0]
elif len(results) == 0:
return None
else:
return results
# UNIT TESTS
if __name__=='__main__':
pass
#db = Db()
#users = db.query('select * from participant;')
#print(users)
#users = db.query('insert into participant (email) values (%s) returning email;', ['shandgp@clarkson.edu'])
#print(users)