我曾尝试从sqlalchemy会话文档中实现会话范围但由于某种原因我得到了一个属性错误:退出 at session_scope作为会话:
代码如下所示:
@contextmanager
def session_scope():
"""Provide a transactional scope around a series of operations."""
session = Session()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
class SearchPhoneTable():
def DateSearch(self,session,StartDate,EndDate):
DateValue=[]
OrderValue=[]
SessionResult=session.query(VideoChange).filter(and_(VideoChange.Date <= EndDate, VideoChange.Date>=StartDate)).all()
for elements in SessionResult:
DateValue.append(elements.Date)
OrderValue.append(elements.ViewsValue)
return DateValue,OrderValue
def PDFOnline2(StartDate,EndDate):
with session_scope as session:
Xaxis,Yaxis=SearchPhoneTable().DateSearch(session,StartDate,EndDate)
for elements in Yaxis:
print(elements)
for elements in Xaxis:
print(elements)
return 0
答案 0 :(得分:1)
你非常接近,你只需要调用上下文管理器功能:
with session_scope() as session:
注意括号。