Python Web Scraping Cookbook
上QQ阅读APP看书,第一时间看更新

How it works

Accessing a MySQL database using the mysql.connector 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. Since we are reading and not writing, there is no need to call commit() on the connection.