当前位置:网站首页>【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'>
边栏推荐
- IO process thread -> thread -> day5
- Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
- 建立树形结构
- closures in js
- 1059 C语言竞赛 (20 分)(C语言)
- 4.如何避免缓存穿透、缓存击穿、缓存雪崩
- Kaggle 入门(Kaggle网站使用及项目复现)
- 2017-06-11 Padavan 完美适配newifi mini【adbyby+SS+KP ...】youku L1 /小米mini
- junit总结
- Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8
猜你喜欢
Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures
高效率科研神器——小软件、大能量
CAD有生僻字如何打出来、如何提交软件相关问题或建议?
shell脚本循环语句
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
tag单调栈-单调栈预备知识-lt.739. 每日温度
2022/08/02 Study Notes (day22) Multithreading
idea uses @Autowired annotation to explain the reasons and solutions
Build your own web page on raspberry pie (1)
typescript43-类型兼容性说明
随机推荐
typescript49-交叉类型
typescript43-类型兼容性说明
Talking about GIS Data (5) - Geographic Coordinate System
[Fine talk] Using native js to implement todolist
ss-3.工程重构
PotPlayer实现上班摸鱼电视自由
Build your own web page on the Raspberry Pi (2)
Js学习笔记(四)
Exception(异常) 和 Error(错误)区别解析
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
如何不耍流氓的做运维之-SHELL脚本
第四次培训
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
Interface Test Framework Practice (4) | Get Schema Assertion
建造者模式(Builder Pattern)
背压机制
JDBC与连接池
Two ways to simulate multi-user login in Jmeter
在树莓派上搭建属于自己的网页(1)
用户密码加密工具