8

Hi I am a new member in stackoverflow. I am currently using sqlAlchemy in flask. Trying to get the matched categories of string provided with the search url. The code of search url is given below:

@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

I tried with httpie with following url: http get http://192.168.1.98:5000/api/v1/search/category?querystr="test"

Error:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) operator does not exist: character varying = boolean

Hoping for a positive response. Thank You.

1
  • Interpolating variables to strings is only available in Python 3.6 and up, and the syntax would be f"{category_param_value}%". Be careful though; interpolating variables to raw SQL strings, HTML etc. is dangerous. In this case you're not using raw SQL, so you're ok. Commented Sep 8, 2016 at 7:59

1 Answer 1

17

You are not using the correct syntax. Also you should format the string you are passing to like.

Change this line:

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

to this:

category = Category.query.filter(Category.title.like(category_param_value + "%")).all()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank u. Its working fine now. I made mistake of using % with argument.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.