-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBHandler.py
32 lines (25 loc) · 982 Bytes
/
DBHandler.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
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
class DBHandler:
Base = declarative_base()
server = 'GAREEBOOO'
database = 'IndustrialWatchFYP'
username = 'sa'
password = '1234'
def __init__(self):
connection_string = f'mssql+pyodbc://{DBHandler.username}:{DBHandler.password}@{DBHandler.server}/{DBHandler.database}?driver=ODBC+Driver+17+for+SQL+Server'
self.engine = create_engine(connection_string, echo=True)
self.Session = sessionmaker(bind=self.engine)
def return_session():
db_handler = DBHandler()
return db_handler.Session()
def check_database_connection():
try:
if DBHandler().engine.connect():
print("Database connection successful.")
else:
print("Database is not Connected")
except Exception as e:
print(f"Error connecting to the database: {str(e)}")
exit()