flask_sqlalchemy数据库遇到的问题
1.如何实现SQLAlchemy的升序降序查询?
源码:asc = public_factory(UnaryExpression._create_asc, ".expression.asc")
desc = public_factory(UnaryExpression._create_desc, ".expression.desc")
例子:
SQL语句:select * from user order by age desc
Python代码:users = User.query.order_by(db.desc(User.age)).all()
2.如何实现SQLAlchemy的Select like的用法?
源码:def like(self, other, escape=None):
"""Implement the ``like`` operator.
In a column context, produces the clause ``a LIKE other``.
E.g.::
select([sometable]).where(sometable.c.column.like("%foobar%"))
:param other: expression to be compared
:param escape: optional escape character, renders the ``ESCAPE``
keyword, e.g.::
somecolumn.like("foo/%bar", escape="/")
.. seealso::
:meth:`.ColumnOperators.ilike`
"""
return self.operate(like_op, other, escape=escape)
例子:
SQL语句:select * from user where username like ‘%john%’
Python代码:users = User.query.filter(Product.username.like(‘%john%’)).all()
总结:其实这些如果能看得懂源代码的英语以及懂得SQL语句,基本可以通过查阅源码解决以上类似的问题。
上一篇: 使用类,实现模块化