首页 > 解决方案 > Flask_SQLAlchemy select, where, scalar, connection_execute

问题描述

我想知道如何使用没有 Flask 扩展但具有在模板中吸引价值(结果)的选项的 SQLalchemy 来编写如下所示的请求:

class Finance(db.Model):
    __tablename__=='MAIN'
    id = db.Column(db.Integer, primary_key=True)
    month = db.Column(db.Integer)
    cost_with_vat = db.Column(db.Integer)
    profit = db.Column(db.Integer)

我想编写相同的请求,就像我可以通过 SQLAlchemy(我在下面写的)一样,没有 Flask 扩展。

profit = select([func.sum(order.columns.profit)])
profit = profit.where(or_(
         order.columns.cost_with_vat>0,
        order.columns.cost_with_vat=='?'))

profit_month = profit.where(order.columns.month==11)
result = connection.execute(profit_month).scalar()

需要在模板中显示结果

@app.route('/')
def index():
    return '''<h1>{{result}}</h1>'''

标签: pythonflask-sqlalchemy

解决方案


推荐阅读