Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Insert Multiple Records Using One Insert Statemen with MySQLdb module (http://www.velocityreviews.com/forums/t647456-insert-multiple-records-using-one-insert-statemen-with-mysqldb-module.html)

anton.ranieri.it@gmail.com 12-06-2008 12:03 PM

Insert Multiple Records Using One Insert Statemen with MySQLdb module
 
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

Albert Hopkins 12-06-2008 12:47 PM

Re: Insert Multiple Records Using One Insert Statemen with MySQLdbmodule
 
On Sat, 2008-12-06 at 04:03 -0800, anton.ranieri.it@gmail.com wrote:
> 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.

> --
> http://mail.python.org/mailman/listinfo/python-list




All times are GMT. The time now is 10:20 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.