0

I have an SQLite database that I want to query. I am using Flask and WTForms to build the query. The search part works fine and passes the information, but the drop down data doesn't seem to work within the query.

I have the following:

@app.route('/', methods=('GET', 'POST'))
def index():
    products = get_products()
    form = SearchForm()
    if form.validate_on_submit():
        order_by = form.orderBy.data
        item = '%' + form.searchFor.data + '%'
        c.execute("SELECT * FROM supplementInfo WHERE (name) LIKE (?) OR (brand) LIKE (?) ORDER BY (?)", (item, item, order_by))
        search_results = c.fetchall()
        return render_template('index.html', search_results=search_results, form=form)
    else:
        return render_template('index.html', form=form, products=products)

Here is the form in case it helps:

class SearchForm(FlaskForm):
    searchFor = StringField('What are you looking for?',validators=[DataRequired()])
    orderBy = SelectField('Order By', choices=[('name', 'Product Name'), ('brand', 'Product Brand'),
                                               ('price', ' Product Price'), ('rating', 'Product Rating')])
cmrussell
  • 1,912
  • 2
  • 8
  • 14
  • we need more details about what doesn't seem to work, is it the `orderBy` parameter ? Does it throw an error ? – PRMoureu Jan 13 '18 at 08:53
  • I was using ? wrong. The ORDER BY can't use the variable in such a manner. The query should have been "... ORDER BY " + order_by... – cmrussell Jan 13 '18 at 15:36

0 Answers0