How it works...

In step 2, we declare an SQL SELECT query. This will return the average number of days a user holds a particular book. If you run this query in the PostgreSQL CLI, you will get a result based on your book data:

+---------------------------------------+-------+
| name | avg |
|---------------------------------------+-------|
| Odoo 12 Development Cookbook | 33 |
| PostgreSQL 10 Administration Cookbook | 81 |
+---------------------------------------+-------+

Step 3 calls the execute() method on the database cursor stored in self.env.cr. This sends the query to PostgreSQL and executes it.

Step 5 uses the fetchall() method of the cursor to retrieve a list of rows selected by the query. This method returns a list of rows. In my case, this is [('Odoo 12 Development Cookbook', 33), ('PostgreSQL 10 Administration Cookbook', 81)]. From the form of the query we execute, we know that each row will have exactly two values, the first being name and the other being the average number of days a user holds a particular book. Then, we simply logged it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset