如何使用"喜欢" sqlAlchemy中的运算符

时间:2016-09-08 07:26:26

标签: python-3.x sqlalchemy flask-sqlalchemy

您好我是stackoverflow的新成员。我目前正在烧瓶中使用sqlAlchemy。尝试获取搜索网址提供的匹配类别的字符串。搜索网址的代码如下:

class Example:
    def __init__(self, parameter):
        if parameter == 0:
            # trivial case, the result is always zero
            self.calc = lambda x: 0.0  # <== replacing a method
        self._parameter = parameter

    def calc(self, x):
        # ... long calculation of result ...
        return result

我尝试使用httpie跟随以下网址:http get http://192.168.1.98:5000/api/v1/search/category?querystr=&#34; test&#34;

错误:

@productapi.route("/search/category", methods=["GET"])
def search_category():
    category_param_value = request.args.get('querystr', None)
    print(category_param_value)
    if category_param_value is None:
        return jsonify(message='Which category you want to search????'), 400
    try:
        category = Category.query.filter_by(
            title=Category.title.like("category_param_value %"))
    except SQLAlchemyError as err:
        return jsonify(message='Category not found.'), 400
    category_list = category_schema.dumps(category)
    return category_list.data, 200

希望得到积极的回应。谢谢。

1 个答案:

答案 0 :(得分:8)

您没有使用正确的语法。您还应该将要传递的字符串格式化为like

更改此行:

category = Category.query.filter_by(title=Category.title.like("category_param_value %"))

到此:

category = Category.query.filter(Category.title.like(category_param_value + "%")).all()