#第一种方法,使用Query中的updata函数更新,然后commit提交
def updata_password(email,new_password):
new_password_hash = generate_password_hash(new_password, method="sha256")
try:
#第一种方法,使用Query中的updata函数更新,然后commit提交
User.query.filter_by(email=email).update({User.password:new_password_hash})
db.session.commit()
except Exception as e:
# 加入数据库commit提交失败,必须回滚!!!
db.session.rollback()
raise e
#第二种方法,直接使用对象user.password赋值更新,然后commit提交
def updata_password(email,new_password):
new_password_hash = generate_password_hash(new_password, method="sha256")
try:
#第二种方法,直接使用对象user.password赋值更新,然后commit提交
#先查询出要更新的用户
user = User.query.filter_by(email=email).first_or_404()
#更新用户的数据
user.password = new_password
#最后提交到数据库
db.session.commit()
except Exception as e:
# 加入数据库commit提交失败,必须回滚!!!
db.session.rollback()
raise e