gpt4 book ai didi

python - 异常后的 SQLAlchemy session

转载 作者:行者123 更新时间:2023-12-01 05:47:55 27 4
gpt4 key购买 nike

在SQLAlchemy中,发生异常后,由于回滚,需要重新设置 session 。因此,我是否应该始终在通话之间设置 session ?

user = User('username', 'Full Name', 'password', 'email',
datetime.now(), 'username')
session.add(user)
try:
session.commit()
except SQLAlchemyError:
pprint('Not quite right...')

# the session needs to be re-instantiated in case of an exception.
# should I always do it or only if there was an exception above?
session = Session()
res = session.query(User).all()
...

最佳答案

鱼:

try:
session.commit()
except SQLAlchemyError:
pprint('Not quite right...')
session.rollback()

Fishing rod .

关于python - 异常后的 SQLAlchemy session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15472658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com