[英]How to update using SQLAlchemy and session?
当我想在我的MyProducts表中添加一行时,我会:
p = models.MyProducts(code="123456")
db.session.add(p)
db.session.commit()
在models.py
里面,我有:
class MyProducts(db.Model):
id = db.Column(db.Integer, primary_key = True, autoincrement=True)
code = db.Column(db.Integer)
quantity = db.Column(db.Integer, default=1)
comment = db.Column(db.String(99999), default="")
pricepaid = db.Column(db.Float(264), default="0.0")
如何更新代码为“123456”的行? 我努力了
updater = models.MyProducts.query.filter_by(code="123456").update({'comment':"greeeeat product"})
db.session.add(updater)
db.session.commit()
但这不起作用。
假设它只是一个产品:
my_product = models.MyProducts.query.filter_by(code=123456).first() # @note: code is Integer, not a String, right?
if my_product:
my_product.comment = "greeeeat product"
db.session.add(my_product)
db.session.commit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.