Insert Multiple Records Using One Insert Statemen with MySQLdb module

  • Thread starter anton.ranieri.it
  • Start date
A

anton.ranieri.it

Hi,

I'd like to insert Multiple Records Using One Insert Statement

inserting one record using one insert statement works
this is the example:

import MySQLdb
conn = MySQLdb.connect(host="localhost",.....)
cursore = conn.cursor()
cursore.execute('INSERT INTO frutta (nome, quantita) VALUES(%s, %s)',
('Pompelmi', 10)
)

but when I try to insert Multiple Records Using One Insert Statement
in this way:

cursore.execute('INSERT INTO frutta (nome, quantita) VALUES(%s, %s)',
('Pompelmi', 10),
('Pompelmi', 10),
('Pompelmi', 10)
)

it doesn't work!

Antonella
 
A

Albert Hopkins

Hi,

I'd like to insert Multiple Records Using One Insert Statement

inserting one record using one insert statement works
this is the example:

import MySQLdb
conn = MySQLdb.connect(host="localhost",.....)
cursore = conn.cursor()
cursore.execute('INSERT INTO frutta (nome, quantita) VALUES(%s, %s)',
('Pompelmi', 10)
)

but when I try to insert Multiple Records Using One Insert Statement
in this way:

cursore.execute('INSERT INTO frutta (nome, quantita) VALUES(%s, %s)',
('Pompelmi', 10),
('Pompelmi', 10),
('Pompelmi', 10)
)

it doesn't work!

You want to use the cursor's .executemany() method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top