-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
52 lines (40 loc) · 1.39 KB
/
config.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
from configparser import ConfigParser
import psycopg2
import pandas as pd
# Credit to http://www.postgresqltutorial.com/postgresql-python/connect/
def config(filename='database.ini', section='postgresql'):
'''function connects to Postgres database using .ini configuaration file'''
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)
# get section, default to postgresql
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
def query(sql_command):
'''function that takes user query and applies it to database'''
conn = None
try:
# read connection parameters
params = config()
# connect to the PostgreSQL server
# print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
# create a cursor
cur = conn.cursor()
data = pd.read_sql(sql_command, conn)
# close the communication with the PostgreSQL
cur.close()
return data
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
# print('Database connection closed.')