首页 > 其他分享 >Flask ORM 学习笔记Part09:数据查询(中)

Flask ORM 学习笔记Part09:数据查询(中)

时间:2023-12-12 19:34:06浏览次数:33  
标签:Part09 Profile Flask age pprint ORM func query app

聚合操作

聚合操作是指对一组值进行汇总、计算或统计的操作。这些操作通常应用于数据库中的列(字段),并用于生成单个标量值(例如平均值AVG、总和SUM、最大值MAX、最小值MIN、计数COUNT等)。

示例代码

from app import app
from model import *
from pprint import pprint
from sqlalchemy import func

with app.app_context():

    # 查询最大值
    pprint("查询最大值")
    pprint("db.session.query(func.max(Profile.age)).scalar()")
    max_age = db.session.query(func.max(Profile.age)).scalar()
    pprint(max_age)

    pprint("Profile.query.with_entities(func.max(Profile.age)).scalar()")
    max_age = Profile.query.with_entities(func.max(Profile.age)).scalar()
    pprint(max_age)

    # 查询平均值
    pprint("查询平均值")
    pprint("db.session.query(func.avg(Profile.age)).scalar()")
    avg_age = db.session.query(func.avg(Profile.age)).scalar()
    pprint(avg_age)

    pprint("Profile.query.with_entities(func.avg(Profile.age)).scalar()")
    avg_age = Profile.query.with_entities(func.avg(Profile.age)).scalar()
    pprint(avg_age)

    # 查询总和
    pprint("查询总和")
    pprint("db.session.query(func.sum(Profile.age)).scalar()")
    sum_age = db.session.query(func.sum(Profile.age)).scalar()
    pprint(sum_age)

    pprint("Profile.query.with_entities(func.sum(Profile.age)).scalar()")
    sum_age = Profile.query.with_entities(func.sum(Profile.age)).scalar()
    pprint(sum_age)

    # 查询最小值
    pprint("查询最小值")
    pprint("db.session.query(func.min(Profile.age)).scalar()")
    min_age = db.session.query(func.min(Profile.age)).scalar()
    pprint(min_age)

    pprint("Profile.query.with_entities(func.min(Profile.age)).scalar()")
    min_age = Profile.query.with_entities(func.min(Profile.age)).scalar()
    pprint(min_age)

    # 查询总和
    pprint("查询总和")
    pprint("db.session.query(func.count(Profile.age)).scalar()")
    total_age = db.session.query(func.sum(Profile.age)).scalar()
    pprint(total_age)

    pprint("Profile.query.with_entities(func.sum(Profile.age)).scalar()")
    total_age = Profile.query.with_entities(func.sum(Profile.age)).scalar()
    pprint(total_age)

    # 查询最大值
    pprint("查询最大值")
    pprint("db.session.query(func.max(Profile.age)).scalar()")
    max_age = db.session.query(func.max(Profile.age)).scalar()
    pprint(max_age)


    # 查询记录数
    pprint("查询记录数")
    pprint("db.session.query(db.func.count(Profile.age)).scalar()")
    profile_count = db.session.query(db.func.count(Profile.age)).scalar()
    pprint(profile_count)

    pprint("Profile.query.with_entities(db.func.count(Profile.age)).scalar()")
    profile_count = Profile.query.with_entities(db.func.count(Profile.age)).scalar()
    pprint(profile_count)

返回结果

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy

代码说明

在这个示例中,我用了 func.max、func.avg、func.min、func.sum、func.count 等函数来执行聚合操作。这些函数可以应用到模型类的属性上,以完成相应的数据库查询操作。

请注意,这些查询返回的是标量值,可直接在模型类上进行调用,而无需额外的 db.session.query。这样的查询语法相对更简洁一些。

with_entities 是 SQLAlchemy 中的方法,用于指定查询返回的字段或表达式。在 Flask-SQLAlchemy 中,这个方法可以用于查询时选择特定的字段或执行聚合操作。这样更贴近模型类的语法风格,使代码更加清晰。


执行方法\终端方法

在SQLAlchemy中有一列方法用于触发实际的 SQL 查询并返回结果,这些方法通常有统一的名字——“执行方法”或“终端方法”。

all()

执行查询并返回所有结果,以列表的形式返回。

from app import app
from model import *
from pprint import pprint

with app.app_context():
    r = Profile.query.all()
    pprint(r)

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy_02

first()

执行查询并返回结果中的第一条记录,如果没有结果则返回 None。

from app import app
from model import *
from pprint import pprint

with app.app_context():
    r = Profile.query.filter(Profile.gender == 'M').first()
    pprint(r

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy_03

one()

执行查询并返回结果中的唯一记录,如果结果不唯一或者没有结果,则引发 sqlalchemy.orm.exc.NoResultFound 或 sqlalchemy.orm.exc.MultipleResultsFound 异常。

from app import app
from model import *
from pprint import pprint

with app.app_context():
    try:
        r = Profile.query.filter(Profile.gender == 'M').one()
        pprint(r)
    except Exception as e:  # gender为M的记录不止一个,导致异常
        pprint(str(e))

    r = Profile.query.filter(Profile.gender == 'F').one()
    pprint(r)

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy_04

scalar()

执行查询并返回结果的第一列的第一个元素,通常用于获取聚合操作的标量值。

scalar()用法已经在上文所体现,不再赘述。

count()

执行查询并返回结果的数量。

同样的,有关于count()的代码在上文中已有所体现。

update(values, synchronize_session='evaluate'):

用于执行更新操作,更新满足条件的记录。

from app import app
from model import *
from pprint import pprint

with app.app_context():
    r = Profile.query.filter(Profile.id == 1).one()
    pprint('更新前数据')
    pprint(r)
    
    Profile.query.filter(Profile.id == 1).update({"fullname": "李四"})
    
    r = Profile.query.filter(Profile.id == 1).one()
    pprint('更新后数据')
    pprint(r)

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy_05

delete(synchronize_session='evaluate')

用于执行删除操作,删除满足条件的记录。

MyModel.query.filter_by(id=1).delete()
db.session.commit()

注意 关于synchronize_session='evaluate'

synchronize_session 参数在 SQLAlchemy 的 update 和 delete 操作中用于指定在执行更新或删除后如何同步会话(session)中的状态。该参数有三个可能的值:

'evaluate':

这是默认值。在执行更新或删除操作后,会话会尝试智能地评估哪些对象的状态应该被同步,从而减少需要刷新对象状态的次数。这样可以提高性能。

'fetch':

在执行更新或删除后,会话会强制从数据库中获取所有相关的对象以确保它们的状态是最新的。这会导致更多的数据库查询,但确保了对象状态的准确性。

'unsynchronize':

在执行更新或删除后,不会自动同步会话中的对象状态。这意味着你需要手动调用 session.expire_all() 或 session.expire(object) 来刷新对象状态。

在 update 和 delete 操作中,默认值 'evaluate' 通常是合适的,因为它会尽力减少不必要的数据库查询,同时仍然保持对象状态的一致性。然而,具体的选择取决于你的应用程序的需求和性能考虑。


join()

用于执行关联查询,将两个表连接起来,并返回结果。

from app import app
from model import *
from pprint import pprint

with app.app_context():
    r = Profile.query.filter(Profile.id == 1).join(Account, Account.id == Profile.account_id).add_entity(Account).all()
    pprint(r)

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy_06

在 SQLAlchemy 中,add_entity 并不是直接用于执行查询的方法,而是用于构建复杂查询时,将其他实体(Entity)添加到查询中的一种方法。它通常用于联合查询(Join)或者复杂查询场景。

在这个join示例中,就是通过add_entity返回两个实例

distinct()

用于执行去重操作,返回查询结果中的唯一记录。

from app import app
from model import *
from pprint import pprint

with app.app_context():
    r = Profile.query.with_entities(Profile.gender).distinct().all()
    pprint(r)

    r = Profile.query.with_entities(Profile.gender, Profile.fullname).distinct().all()
    pprint(r)

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy_07

group_by(*args)

用于执行分组操作,将结果按照指定的列进行分组。

from app import app
from model import *
from pprint import pprint
from sqlalchemy import func

with app.app_context():
    r = db.session.query(Profile.gender, func.count().label('count')).group_by(Profile.gender).all()
    pprint(r)

    r = Profile.query.with_entities(Profile.gender, func.count().label('count')).group_by(Profile.gender).all()
    pprint(r)

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy_08

having(criterion)

用于在分组后的结果上执行条件筛选。

from app import app
from model import *
from pprint import pprint
from sqlalchemy import func

with app.app_context():
    r = db.session.query(Profile.gender, func.count().label('count')).group_by(Profile.gender)\
        .having(func.count() > 1).all()
    pprint(r)

    r = Profile.query.with_entities(Profile.gender, func.count().label('count')).group_by(Profile.gender)\
        .having(func.count() > 1).all()
    pprint(r)

Flask ORM 学习笔记Part09:数据查询(中)_sqlalchemy_09

标签:Part09,Profile,Flask,age,pprint,ORM,func,query,app
From: https://blog.51cto.com/quietguoguo/8790181

相关文章

  • pure-admin pnpm  ERR_PNPM_FROZEN_LOCKFILE_WITH_OUTDATED_LOCKFILE  Cannot perf
    事情是这样的,用的开源pure-admin的框架,用的是pnpm,本地环境都是可以的,但是发布到生成就报以下错误  然后看部署参数,是这样的,强制用了lock文件,本来也没问题 报错的意思是json文件跟pnpm-lock.json文件不匹配但是本地看着是匹配的,随便挑选几个包版本看着也是一致的然后......
  • 【WinForm详细教程五】WinForm中的MenuStrip 、ContextMenuStrip 、ToolStrip、Status
    原文链接:https://blog.csdn.net/QH2107/article/details/1341922511.MenuStripMenuStrip作为一个容器可以包含多个菜单项。MenuStrip的重要属性包括:Name:菜单的名字Dock:菜单的停靠位置Items:菜单项的集合ToolStripMenuItemToolStripMenuItem是MenuStrip中的菜单项,可以有以下......
  • Transformer架构在大型语言模型(LLM)中的应用与实践
     Transformer架构是当今最前沿的语言模型技术之一,它已经在谷歌的BERT、OpenAI的GPT系列中取得了显著的成就。这一架构之所以独特,是因为它打破了传统的序列处理模式,引入了创新的“自注意力”机制。 Transformer架构的核心是自注意力机制,它使模型能够识别和重视输入数据中不同......
  • 如何将 performance_schema 中的 TIMER 字段转换为日期时间
    问题最近有好几个朋友问,如何将performance_schema.events_statements_xxx中的TIMER字段(主要是TIMER_START和TIMER_END)转换为日期时间。因为TIMER字段的单位是皮秒(picosecond),所以很多童鞋会尝试直接转换,但转换后的结果并不对,看下面这个示例。mysql> select * from per......
  • .net formwork WebApi 跨域问题
    ASP.NETFormwork Api/ASP.NetCoreApi 做比较。有关 Global.asax、FilterConfig.cs和RouteConfig.cs等都被Program.cs和Startup.cs两个类取而代之.程序中把Program.cs作为Web应用程序的入口,程序启动的时候会调用Startup.cs类。Startup.cs作用就是,对项目......
  • django orm 软删除联表查询 需要显示软删除条件
    比如课程学习required_ok=UserLesson.objects.values('uuid').distinct().filter(user_id=self.request.user_id,).filter(lesson__course__course_bx_org__org_id__in=org_ids,).filter(Q(lesson__course__......
  • [论文阅读] Replacing softmax with ReLU in Vision Transformers
    Pretitle:ReplacingsoftmaxwithReLUinVisionTransformersaccepted:Arxiv2023paper:https://export.arxiv.org/abs/2309.08586code:None关键词:attention,parallelization阅读理由:GoogleDeepmind,标题挺有意思Idea序列缩放能缓解ReLU等激活函数在attention中替......
  • Flask ORM 学习笔记Part09:数据查询(上)
    前面的笔记,从Marshmallow开始就稍微有些跑题,今天记录一下如何使用Flask-SQLAlchemy进行数据查询。查询语法糖在前文中,有定义过一系列的model类,这里一Account类作为示例。fromappimportappfrommodelimport*fromschemaimport*frompprintimportpprint#fromsqlalchem......
  • relay interface (formerly relayfs) 【ChatGPT】
    https://www.kernel.org/doc/html/v6.6/filesystems/relay.html#relay-interface-formerly-relayfsRelayInterface(formerlyrelayfs)介绍Relay接口提供了一种方式,让内核应用能够通过用户定义的“中继通道”高效地将大量数据从内核传输到用户空间。一个“中继通道”是一种......
  • unity Transform 的 Rotate(xAngle: float, yAngle: float, zAngle: float, relativeT
    publicclassdemoword2:MonoBehaviour{//StartiscalledbeforethefirstframeupdatevoidStart(){//transform.Rotate(60,70,80,Space.World);//eulerAngles.z度围绕z轴,eulerAngles.x度围绕x轴,eulerAngles.y度围绕y轴//......