flask学习笔记09
过滤条件
首先,先建立一个数据库和数据表。
equals过滤
items = session.query(Shelf).filter(Shelf.author == '罗贯中').all()
for item in items:
print(item)
{'id': 1, 'name': '三国演义', 'author': '罗贯中', 'pricr': Decimal('89.90'), 'status': '有货'}
not equals过滤
items = session.query(Shelf).filter(Shelf.author != '罗贯中').all()
for item in items:
print(item)
{'id': 2, 'name': '水浒传', 'author': '施耐庵', 'pricr': Decimal('67.80'), 'status': '有货'}
{'id': 3, 'name': '西游记', 'author': '吴承恩', 'pricr': Decimal('99.90'), 'status': '无货'}
{'id': 4, 'name': '红楼梦', 'author': '曹雪芹', 'pricr': Decimal('80.00'), 'status': '有货'}
like过滤
items = session.query(Shelf).filter(Shelf.author.like('罗贯%')).all()
for item in items:
print(item)
{'id': 1, 'name': '三国演义', 'author': '罗贯中', 'pricr': Decimal('89.90'), 'status': '有货'}
这种like方式有些像模糊搜索。
in_过滤
items = session.query(Shelf).filter(Shelf.author.in_(['罗贯中','曹雪芹'])).all()
for item in items:
print(item)
{'id': 1, 'name': '三国演义', 'author': '罗贯中', 'pricr': Decimal('89.90'), 'status': '有货'}
{'id': 4, 'name': '红楼梦', 'author': '曹雪芹', 'pricr': Decimal('80.00'), 'status': '有货'}
notin_过滤
有in就有notin,只不过要注意notin有两种写法
items = session.query(Shelf).filter(~Shelf.author.in_(['罗贯中','曹雪芹'])).all()
for item in items:
print(item)
items = session.query(Shelf).filter(Shelf.author.notin_(['罗贯中','曹雪芹'])).all()
for item in items:
print(item)
结果是一样的
{'id': 2, 'name': '水浒传', 'author': '施耐庵', 'pricr': Decimal('67.80'), 'status': '有货'}
{'id': 3, 'name': '西游记', 'author': '吴承恩', 'pricr': Decimal('99.90'), 'status': '无货'}
{'id': 2, 'name': '水浒传', 'author': '施耐庵', 'pricr': Decimal('67.80'), 'status': '有货'}
{'id': 3, 'name': '西游记', 'author': '吴承恩', 'pricr': Decimal('99.90'), 'status': '无货'}
is_过滤
items = session.query(Shelf).filter(Shelf.status.is_('None')).all()
for item in items:
print(item)
and过滤
and过滤有三种写法
items = session.query(Shelf).filter(Shelf.status == '有货', Shelf.author == '施耐庵').all()
for item in items:
print(item)
items = session.query(Shelf).filter(Shelf.status == '有货').filter(Shelf.author == '施耐庵').all()
for item in items:
print(item)
items = session.query(Shelf).filter(and_(Shelf.status == '有货',Shelf.author == '施耐庵')).all()
for item in items:
print(item)
上面三个语句都是一样的结果
{'id': 2, 'name': '水浒传', 'author': '施耐庵', 'pricr': Decimal('67.80'), 'status': '有货'}
or_过滤
items = session.query(Shelf).filter(or_(Shelf.status == '有货',Shelf.author == '施耐庵')).all()
for item in items:
print(item)
外键约束
大家可以把他理解成一种关系,像我下面写的书籍和作者的关系。
class Author(Base):
__tablename__ = 'author'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(25), nullable=False)
def __str__(self):
return str(dict(zip(['id', 'name'],[self.id, self.name])))
class Book(Base):
__tablename__ = 'book'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(25), nullable=False)
price = Column(DECIMAL(5, 2))
status = Column(Enum('有货', '无货'))
uid = Column(Integer,ForeignKey('author.id', ondelete='RESTRICT'))
uid = Column(Integer,ForeignKey(‘author.id’, ondelete=‘RESTRICT’))这条语句就把两个表联合起来了
我们看一下这两个表格
我们还要注意外键约束
RESTRICT
父表数据被删除时会阻止删除并报错,这个约束也是默认约束。
uid = Column(Integer,ForeignKey('author.id', ondelete='RESTRICT'))
NO ACTION
几乎同上,
CASCADE
级联操作,删除父表的记录,子表的记录也会删除
SET NULL
父表记录被删除,子表记录的uid会设置为NULL
一对多
其实一对多就是一个父表对应多个子表,和多个子类继承同一个父类一样的思路。