Executing raw SQL queries
Most of the time, you can perform the operations you want by using Odoo’s ORM – for example, you can use the search()
method to fetch records. However, sometimes, you need more; either you cannot express what you want using the domain syntax (for which some operations are tricky, if not downright impossible) or your query requires several calls to search()
, which ends up being inefficient.
This recipe shows you how to use raw SQL queries to get the name and amount a user keeps in a particular room.
Getting ready
For this recipe, we will use the my_hostel
module from the previous recipe. For simplicity, we will just print the results in a log, but in real scenarios, you will need to use the query result in your business logic. In Chapter 9, Backend Views, we will display the result of this query in the user interface.
How to do it...
To get information about the name and amount a user keeps in a particular room, you need to perform...