-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
47 lines (40 loc) · 1.38 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
41
42
43
44
45
46
import psycopg2
import config
class Database:
"""PostgreSQL Database class."""
def __init__(self):
self.host = config.DATABASE_HOST
self.username = config.DATABASE_USERNAME
self.password = config.DATABASE_PASSWORD
self.port = config.DATABASE_PORT
self.dbname = config.DATABASE_NAME
self.conn = None
# print(f'Path: {config.PATH}')
print("Database obj created!")
print(f'Host={self.host}')
def connect(self):
"""Connect to a Postgres database."""
if self.conn is None:
try:
self.conn = psycopg2.connect(
host=self.host,
user=self.username,
password=self.password,
port=self.port,
dbname=self.dbname
)
except psycopg2.DatabaseError as e:
# LOGGER.error(e)
print(e)
raise e
finally:
# LOGGER.info('Connection opened successfully.')
print('Connection opened successfully.')
def execute_query(self, query):
"""Run a SQL query to select rows from table."""
self.connect()
with self.conn.cursor() as cur:
cur.execute(query)
records = [row for row in cur.fetchall()]
cur.close()
return records