-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
38 lines (31 loc) · 994 Bytes
/
Database.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
import json
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# Read db configuration from JSON
credentials_directory = os.path.dirname(__file__)
credentials_path = os.path.join(credentials_directory, 'Credentials/pg_config.json')
with open(credentials_path, 'r') as json_file:
data = json.load(json_file)
user = data["user"]
password = data["password"]
database = data["database"]
server = data["server"]
charset = "utf-8"
db_url = f"postgresql://{user}:{password}@{server}/{database}"
# Function to Create Engine
def creating_engine():
engine = create_engine(db_url)
return engine
# Function to create the sessions
def creating_session(engine):
Session = sessionmaker(bind=engine)
session = Session()
return session
# Function to close the session
def closing_session(session):
session.close()
# Function to Dispose Engine
def disposing_engine(engine):
engine.dispose()
print("engine closed")