繁体   English   中英

如何使用SQLAlchemy和session更新?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM