• Flask数据库_query函数的使用


    Flask数据库_query函数的使用

    一.query函数的参数介绍

    • 模型名
      指定查找这个模型中所有的属性(对应查询表为全表查
    • 模型中的属性。
      可以指定只查找某个模型的其中几个属性
    • 聚合函数
      • func.count:统计行的数量。
      • func.avg:求平均值。
      • func.max:求最大值。
      • func.min:求最小值。
      • func.sum:求和。
    注意:
    func 上,其实没有任何聚合函数。但是因为他底层做了一些 魔术,只要mysql中有的聚合函数,都可以通过func调用。

    二.实例

    db_utils.py:
    #coding=utf-8
    
    
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import sessionmaker
    
    USER = 'xx'
    PWD = 'xx'
    HOST = '127.0.0.1'
    PORT = 3306
    DATA_BASE = 'flask_db'
    
    DB_URL = f'mysql+pymysql://{USER}:{PWD}@{HOST}:{PORT}/{DATA_BASE}'
    
    # 创建一个引擎
    engine = create_engine(DB_URL)
    Base = declarative_base(engine)
    Session = sessionmaker(engine)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    main.py:
    from turtle import title
    from db_utils import Base,Session
    from sqlalchemy import Column,Integer,String,func
    from random import randint
    
    
    class Item(Base):
        __tablename__ = 't_items'
        id = Column(Integer,primary_key=True,autoincrement=True)
        title = Column(String(32))
        price = Column(Integer)
    
    def add_data():
        with Session() as s:
            for i in range(10):
                item = Item(title=f'产品:{i+1}',price=randint(1,100))
                s.add(item)
            s.commit()
    
    def query_data():
        with Session() as s:
            item = s.query(Item).all()
            for i in item:
                print(i.title,i.price)
    
    def query_data1():
        with Session() as s:
            item = s.query(Item.price).all()
            for i in item:
                print(i.price)
    
    def query_data2():
        with Session() as s:
            # n = s.query(func.count(Item.id)).first()
            # n = s.query(func.max(Item.price)).first()
            # n = s.query(func.avg(Item.price)).first()
            n = s.query(func.sum(Item.price)).first()
            print(n)
    
    if __name__ == '__main__':
        # Base.metadata.create_all()
        # add_data()
        query_data2()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    C++重载底层原理
    如何从第一性原则的原理分解数学问题
    Ansible playbook详解
    socket学习二、accept、read、write函数详解
    【无标题】ethtool程序,从用户态调用内核态调用分析
    你知道吗?chrome自动更新到104版本,居然引起Java服务内存泄漏
    二叉排序树查询删除结点和删除结点的父节点(代码实现) [Java]
    GPT-3.5发布:大型语言模型的进化与挑战
    Rust ?运算符 Rust读写txt文件
    PDF标准详解(三)—— PDF坐标系统和坐标变换
  • 原文地址:https://blog.csdn.net/qq_55961861/article/details/126838303