当前位置:网站首页>【Flask】Flask-SQLAlchemy的增删改查(CRUD)操作
【Flask】Flask-SQLAlchemy的增删改查(CRUD)操作
2022-08-03 05:10:00 【중둑【上瘾】】
增加(Create)
1.创建 Python 对象的一个实例
2.把添加实例告诉会话
3.提交会话
from models import User
user1 = User('admin', '[email protected]')
db.session.add(user1)
db.session.commit()
修改(Update)
1.查询要修改的对象的实例
2.把修改实例属性的操作告诉会话
3.提交会话
from models import User
user1 = User.query.filter(User.username == 'admin').first()
user1.username = 'admin_new'
db.session.commit()
删除(Delete)
1.查询要删除的对象的实例
2.把删除操作告诉会话
3.提交会话
from models import User
user1 = User.query.filter(User.username == 'admin').first()
db.session.delete(user1)
db.session.commit()
检索(Retrieve)
参照官网给出的示例进行总结,假定数据库中有如下数据:
| id | username | |
|---|---|---|
| 1 | admin | [email protected] |
| 2 | peter | [email protected] |
| 3 | guest | [email protected] |
通过用户名查询用户,查询一个不存在的用户名返回 None。
>>> peter = User.query.filter_by(username='peter').first()
>>> peter.id
1
>>> peter.email
u'[email protected]'
>>> missing = User.query.filter_by(username='missing').first()
>>> missing is None
True
查询email属性以@example.com结尾的用户,.all()将返回一个对象组成的列表
>>> User.query.filter(User.email.endswith('@example.com')).all() # 返回一个列表
[<User u'admin'>, <User u'guest'>]
按某种规则对用户排序
>>> User.query.order_by(User.username)
[<User u'admin'>, <User u'guest'>, <User u'peter'>]
限制返回用户的数量。
>>> User.query.limit(1).all()
[<User u'admin'>]
用主键查询用户。
>>> User.query.get(1)
<User u'admin'>
边栏推荐
- Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
- 接口和抽象
- 集合框架知识
- 【Harmony OS】【FAQ】Hongmeng Questions Collection 1
- Tag stack - stack monotonically preparatory knowledge - lt. 739. The daily temperature
- 1. 两数之和
- Concepts and Methods of Exploratory Testing
- CAD有生僻字如何打出来、如何提交软件相关问题或建议?
- Super handy drawing tool is recommended
- Interface testing framework of actual combat (2) | interface request assertion
猜你喜欢

Apache DolphinScheduler版本2.0.5分布式集群的安装

2022/08/02 Study Notes (day22) Multithreading

高可用 两地三中心

第四次培训

High availability, two locations and three centers

接口测试框架实战 | 流程封装与基于加密接口的测试用例设计

shell script loop statement

Talking about GIS Data (5) - Geographic Coordinate System

Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8

Two ways to simulate multi-user login in Jmeter
随机推荐
斐讯K2路由编译Padavan华硕固件和心得
[Harmony OS] [ArkUI] ets development graphics and animation drawing
1069 微博转发抽奖 (20 分)(C语言)
idea uses @Autowired annotation to explain the reasons and solutions
ss-4.2 多个eureka集群案例
Where is the value of testers
1094 谷歌的招聘 (20 分)
High availability, two locations and three centers
1054 求平均值 (20 分)
Presto installation and deployment tutorial
2017-06-11 Padavan 完美适配newifi mini【adbyby+SS+KP ...】youku L1 /小米mini
【Biotin Azide|cas:908007-17-0】Price_Manufacturer
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
Ali cloud object storage oss private barrels to generate links
PotPlayer实现上班摸鱼电视自由
【Harmony OS】【FAQ】Hongmeng Questions Collection 1
阿里云对象存储oss私有桶生成链接
Interface Test Framework Practice (4) | Get Schema Assertion
网络流媒体下载的 10 种方法(以下载 Echo 音乐为例)
Interface test Mock combat (2) | Combined with jq to complete batch manual Mock