This method prepares a database operation (query or command) and
then execute it against all parameter sequences or mappings
found in the sequence seq_of_params.
The executemany() is simply iterating through
the sequence of parameters calling the
execute() method. Inserting data, however, is
optimized by batching them using the multiple rows syntax.
In the following example we are inserting 3 records:
data = [
('Jane', date(2005, 2, 12)),
('Joe', date(2006, 5, 23)),
('John', date(2010, 10, 3)),
]
stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
cursor.executemany(stmt, data)
In the preceding example, the
INSERT statement sent to MySQL
would be as follows:
INSERT INTO employees (first_name, hire_date)
VALUES ('Jane', '2005-02-12'), ('Joe', '2006-05-23'), ('John', '2010-10-03')
Note that it is not possible to execute multiple statements
using the executemany() method. Doing so
raises an InternalError exception.

User Comments
Add your own comment.