Flask-SQLalchemy
Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application.
a quick example
1 | from flask import Flask |
Select, Insert, Delete
Querying Records
-
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
44
45
46
47# Select the first
User.query.first()
User.query.get(1) # According to primary key
# Select all
User.query.all()
# Get the number of data quantity
User.query.count()
# Get the data with id 4
User.query.filter_by(id=4).all()
User.query.filter(User.id == 4).all()
# startwith/endwith
User.query.filter(User.name.startswith('xxx')).all()
User.query.filter(User.name.endswith('xxx')).all()
# contain
User.query.filter(User.name.contains("n")).all()
User.query.filter(User.name.like("%n%g")).all()
User.query.filter(User.id.in_([1, 3, 5, 7, 9])).all()
# Intersection condition
from sqlalchemy import and_
User.query.filter(and_(User.name == 'small', User.age = 18)).all()
# Union condition
from sqlalchemy import or_
User.query.filter(or_(User.name == 'small', User.age = 18)).all()
# Not contain
from sqlalchemy import not_
User.query.filter(not_(User.name == 'small')).all()
# order (Default ascending)
User.query.order_by(User.age, User.id.desc()).limit(5).all()
# Paging query
"""
pn.items: The amount of data retrieved
pn.page : The current page
pn.pages: The amount of pages
"""
pn = User.query.paginate(page, items)