-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
23 lines (19 loc) · 741 Bytes
/
db.py
File metadata and controls
23 lines (19 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import os
# Crear un motor de base de datos y una sesión para hacer consultas
SQL_ALCHEMY_DATABASE_URL = ("mssql+pyodbc://{}:{}@{}/{}"
"?driver=ODBC+Driver+17+for+SQL+Server").format(
os.environ['DB_USER'],
os.environ['DB_PASSWORD'],
os.environ['DB_HOST'],
os.environ['DB_NAME'],
)
def get_db():
try:
engine = create_engine(SQL_ALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(bind=engine)
session = SessionLocal()
yield session
finally:
session.close()