-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_manager.py
More file actions
44 lines (35 loc) · 985 Bytes
/
db_manager.py
File metadata and controls
44 lines (35 loc) · 985 Bytes
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
41
42
import psycopg2
#23456789012345
class DbManager:
def __init__(self):
self.conn_str = "dbname=jnv user=postgres password=postgres"
# def __init__(self, dbname, user, password):
# self.dbname = dbname
# self.user = user
# self.password = password
def fetch_all(self, sql):
"""Fetches all records."""
con = psycopg2.connect(self.conn_str)
cur = con.cursor()
cur.execute(sql)
records = cur.fetchall()
con.commit
cur.close
con.close
return records
def fetch_one(self, sql):
"""Fetches 1 record."""
con = psycopg2.connect(self.conn_str)
cur = con.cursor()
cur.execute(sql)
record = cur.fetchone()
con.commit
cur.close
con.close
return record
## TODO: Unit test
if __name__ == "__main__":
sql = """ SELECT * FROM users """
dbm = DbManager()
print(dbm.fetch_all(sql))
#