-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbconnector.py
More file actions
35 lines (27 loc) · 882 Bytes
/
dbconnector.py
File metadata and controls
35 lines (27 loc) · 882 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
from configparser import ConfigParser
import psycopg2
# get data for connection from file
def config(filename='database.ini', section='postgresql'):
try:
parser = ConfigParser()
parser.read('database.ini')
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
except FileNotFoundError as error:
print(error)
# connecting do DB
def connect_to_db():
conn = None
try:
params = config()
print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
return conn
except (Exception, psycopg2.DatabaseError) as error:
print(error)