In any web application, it is important to be able to search the database for records based on some 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.
We have been implementing some level of search 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 more advanced level and search on the basis of name and category.
The following is a method that searches in our catalog application for name, price, company, and category. We can search for any one or multiple criterion (except for the search on category, which can only be searched alone). Notice that we have different expressions for different values. For a float value in price, we can search for equality, while in the case of a string, we can search using like
. Also, carefully note...