[DB-SIG] PySQL/insert a list of tuples....

Marcos Sánchez Provencio msanchez@grupoburke.com
24 Aug 2002 12:13:01 +0200


El vie, 23-08-2002 a las 10:24, Denis Cartier Millon escribió:
> YES BUT two problèmes :
> 
> 1:  the "%"  : %s,%i,..... % <list og params>  (format like in C) ignore 
> the none (Python) null (SQL),
> 2:  this solution needs to now the numbers and the types of parameters...

You must not merge the string with %, but pass the parameters to the
function

executemany('sql blablabla '   ,    (list of parameters in tuples) )
I believe you may use %s for every parameter, whatever its type.


> 
> before i use the module pg.py (module postgresql) and pg.inserttable 
> (<table>,<liste o tuples>) :  inserttable (Mytable,x) but this solution
> don't insert the none value.
> 
> 
> with  <?>
> 
> cur.executemany  ("insert into Mytable values (?,?,?)", list of params)  
> (example in LINUX JOURNAL
>     the mappage is  OK by the the dbapi
>     but i must know the numbers of params.
> 
> is the syntaxe :
> 
> cur.executemany ("insert into Mytable values (?,?,?)", x)
>  # where x is a list of tuples is OK ???
> # if this is OK i win because i can build  the string "insert into 
> Mytable values(?,?,?....)
> 
> 

I believe that is the solution.

> Marcos Sánchez Provencio a écrit:
> 
> >El jue, 22-08-2002 a las 15:10, Denis Cartier Millon escribió:
> >  
> >
> >>I  want insert a sequence of sequence (list og tuples) in a table ,
> >>(x comme from the script select * from Mytable with an fethchall)
> >>
> >>and the script is :
> >>cur=conn.cursor()
> >>cur.executemany("insert into Mytable values (%s)",x) # is there a 
> >>solution thanks.....
> >>
> >>    
> >>
> >Is there a problem?
> >
> >But seriously, you have to give as many %s as column values you want
> >inserted in your sql 
> >
> >cur.executemany("insert into Mytable (col1,col2,col3) values
> >(%s,%s,%s)",x)
> >
> >
> >_______________________________________________
> >DB-SIG maillist  -  DB-SIG@python.org
> >http://mail.python.org/mailman/listinfo/db-sig
> >
> >  
> >
>