from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker class Foootball(object): def __init__(self): self.db = self._get_db_session() def _get_db_session(self): _engine = create_engine("mysql+pymysql://test:123456@localhost/lottery?charset=utf8", pool_pre_ping=True, pool_recycle=3600) _DbSession = sessionmaker(bind=_engine) return _DbSession() def __enter__(self): return self def __exit__(self, type, value, trace): self.db.close()