-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
38 lines (34 loc) · 1.11 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
import psycopg2
from psycopg2.extras import NamedTupleCursor
class PostgresDatabase:
def __init__(self, name='movie-search-engine', user='fabio'):
self.name = name
self.user = user
self.last_executed_query = ''
self.results = []
def query(self, q, args=()):
self.con = psycopg2.connect(dbname=self.name, user=self.user, cursor_factory=NamedTupleCursor)
self.cur = self.con.cursor()
self.last_executed_query = self.cur.mogrify(q, args).decode('utf-8')
try:
self.cur.execute(q, args)
self.con.commit()
self.results = self.cur.fetchall()
except psycopg2.ProgrammingError as e:
print(e)
self.results = []
except Exception as e:
print(e)
if self.cur:
self.con.rollback()
finally:
if self.cur:
self.cur.close()
if self.con:
self.con.close()
return self.results
def close(self):
if self.cur:
self.cur.close()
if self.con:
self.con.close()