-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostgresHandler.py
executable file
·71 lines (61 loc) · 2.42 KB
/
PostgresHandler.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
__author__ = 'Marlon'
__version__ = '1.0.0.0'
import sys
sys.path.append("...")
from sqlalchemy import create_engine
import configs.ConfigHandler as conf
pg_conf = conf.get_conf('config.ini','postgres')
class PostgresHandler():
def __init__(self, db):
self.db = db
if db == 'db1':
try:
self.database_name = pg_conf['database_1_name']
self.database_username = pg_conf['database_1_username']
self.database_password = pg_conf['database_1_password']
self.database_host = pg_conf['database_1_host']
self.database_port = pg_conf['database_1_port']
except Exception as err:
print (err)
else:
try:
self.database_name = pg_conf['database_2_name']
self.database_username = pg_conf['database_2_username']
self.database_password = pg_conf['database_2_password']
self.database_host = pg_conf['database_2_host']
self.database_port = pg_conf['database_2_port']
except Exception as err:
print (err)
raise
# try:
# conn = " "#Todo #psycopg2.connect(database=database, user=user, password=password, host=host, port=port)
# except psycopg2.InterfaceError as err:
# print exc.message
# conn = psycopg2.connect(database=database, user=user, password=password, host=host, port=port)
# pass
# except Exception, err:
# print err
# pass
#
# conString_1 = 'postgresql://{}:{}@{}:{}/{}'.format(database_1_username, database_1_password , database_1_host ,database_1_port , database_1_name)
# try:
# engine_1 = create_engine(conString_1)
# connection_1 = engine_1.connect()
# except Exception as err:
# print (err)
def initiate(self):
conString = 'postgresql://{}:{}@{}:{}/{}'.format(self.database_username, self.database_password , self.database_host , self.database_port , self.database_name)
try:
self.engine = create_engine(conString)
self.connection = self.engine.connect()
except Exception as err:
print (err)
def execute_query(self, query):
result = self.connection.execute(query)
self.engine.dispose()
columns = result.keys()
results = []
if columns:
for row in result:
results.append(dict(zip(columns, row)))
return results