Introducing the executemany() method
As seen in previous chapters, MySQLdb
provides multiple ways to retrieve data such as: fetchall()
, fetchmany()
, and fetchone()
. These allow us to use different strategies for processing data according to how many records we have to process and the environment in which we are working. MySQLdb
also provides more than one way to insert information.
Up to now, we have used Cursor.execute()
whenever we needed to insert data. As shown previously, however, executing single statements in iteration greatly increases the processing requirements of the program. For this reason, MySQL for Python provides another method for Cursor
objects—executemany()
.
To be sure, executemany()
is effectively the same as simple iteration. However, it is typically faster. It provides an optimized means of affecting INSERT
and REPLACE
across multiple rows.