How it works

Accessing a PostgreSQL database using the psycopg2 library as we did involves the use of two classes from the library: connect and cursor. The connect class opens and manages a connection with the database server. From that connection object, we can create a cursor object. This cursor is used for reading and writing data using SQL statements.

In the first example, we used the cursor to insert nine records into the database.  Those records are not written to the database until the commit() method of the connection is called.  This executes the writes of all the rows to the database.

Reading data uses a similar model, except that we execute an SQL query (SELECT) using the cursor and iterate across the rows that were retrieved. ...

Get Python Web Scraping Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.