-
Notifications
You must be signed in to change notification settings - Fork 3
/
dbio.py
52 lines (39 loc) · 1.13 KB
/
dbio.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
47
48
49
50
51
52
# -*- coding: utf-8 -*-
from dotenv import load_dotenv
load_dotenv('.env')
import logging
logging.basicConfig(filename='logs/db.connection.log', level=logging.WARNING)
import os
import psycopg2
db = {
'dbname': os.getenv('DB_NAME'),
'user': os.getenv('DB_USER'),
'host': os.getenv('DB_HOST'),
'password': os.getenv('DB_PASSWORD'),
'port': os.getenv('DB_PORT'),
}
def connect_to_db():
try:
connection_parameters = 'dbname={} user={} host={} password={} port={}'.format(
db['dbname'], db['user'], db['host'], db['password'], db['port']
)
conn = psycopg2.connect(connection_parameters)
except Exception as e:
print('problem connecting to the database!')
logging.error(e)
else:
return conn, conn.cursor()
def close_connection(cur, conn):
cur.close()
conn.close()
def get_query(query):
conn, cur = connect_to_db()
cur.execute(query)
data = cur.fetchall()
close_connection(conn, cur)
return data
def update_query(query):
conn, cur = connect_to_db()
cur.execute(query)
conn.commit()
close_connection(conn, cur)