Skip to content

Instantly share code, notes, and snippets.

@zehpatricio
Created March 30, 2022 18:48
Show Gist options
  • Save zehpatricio/0e61f4121eb95e0f8dc162882f84f45e to your computer and use it in GitHub Desktop.
Save zehpatricio/0e61f4121eb95e0f8dc162882f84f45e to your computer and use it in GitHub Desktop.
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_alchemy.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
Base.metadata.create_all(bind=engine)
db = SessionLocal()
try:
return db
finally:
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment