Chapter 6. Inserting Multiple Entries

When we deal with large amounts of data that are all going into the same database, running single instances of individual INSERT commands can take a ridiculous amount of time and waste a considerable amount of I/O. What we need is a way to insert multiple items in one go.

In this chapter, we will look at the following:

  • How iteration can help us execute several individual INSERT statements rapidly
  • Using executemany() to submit several INSERT statements in one go
  • When not to use executemany()
  • Throttling how much data is inserted at a time

At the end of the chapter, we will put these lessons to use in writing a conversion program that inserts the contents of a comma-separated value (CSV) file into MySQL.

The problem ...

Get MySQL for Python 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.