def get_session(dbpath, scoped=False): # , enable_fk_if_sqlite=True):
"""
Create an sql alchemy session for IO db operations
:param dbpath: the path to the database, e.g. sqlite:///path_to_my_dbase.sqlite
:param scoped: boolean (False by default) if the session must be scoped session
"""
# init the session:
engine = create_engine(dbpath)
Base.metadata.create_all(engine) # @UndefinedVariable
# enable fkeys if sqlite. This can be added also as event listener as outlined here:
# http://stackoverflow.com/questions/13712381/how-to-turn-on-pragma-foreign-keys-on-in-sqlalchemy-migration-script-or-conf
# NOT implemented YET. See models.py
if not scoped:
# create a configured "Session" class
session = sessionmaker(bind=engine)
# create a Session
return session()
# return session
else:
session_factory = sessionmaker(bind=engine)
return scoped_session(session_factory)
评论列表
文章目录