SQLAlchemy equivalent to SQL “LIKE” statement

前端 未结 5 658
借酒劲吻你
借酒劲吻你 2020-12-02 21:50

A tags column has values like \"apple banana orange\" and \"strawberry banana lemon\". I want to find the SQLAlchemy equivalent statement to

SELECT * FROM ta         


        
5条回答
  •  情深已故
    2020-12-02 22:34

    Each column has like() method, which can be used in query.filter(). Given a search string, add a % character on either side to search as a substring in both directions.

    tag = request.form["tag"]
    search = "%{}%".format(tag)
    posts = Post.query.filter(Post.tags.like(search)).all()
    

提交回复
热议问题