Implementing SQL-based searching
In any web application, it is important to be able to search a database for records based on certain criteria. In this recipe, we will go through how to implement basic SQL-based searching in SQLAlchemy. The same principle can be used to search any other database system.
Getting ready
We have implemented some level of search functionality in our catalog application from the beginning. Whenever we show the product page, we search for a specific product using its ID. We will now take it to a slightly more advanced level and search on the basis of name and category.
How to do it...
The following is a method that searches in our catalog application for name, price, company, and category. We can search for any one criterion, or multiple criteria (except for a search by category, which can only be searched alone). Note that we have different expressions for different values. For a float value in price
, we can search for equality, and in the case...