Cursors in a Loop

Chris cwitts at gmail.com
Sat Jan 5 13:00:17 EST 2008


On Jan 4, 4:32 pm, Carsten Haese <cars... at uniqsys.com> wrote:
> On Fri, 2008-01-04 at 00:03 -0800, Chris wrote:
> > You should bind all variables to save the pool.
>
> > cursor = connection.cursor()
> > cursor.executemany("""insert into as_siebel_hosts_temp
> >                       values (:whole, :lot, :of, :bind, :variables)
> >                    """
> >                   ,[(i,)[0] for i in hostlist]
> >                   )
> > connection.commit()
> > connection.close()
>
> Huh? In the OP's example, the table one has one column. I'll openly
> admit that I don't know anything about Oracle, but that code doesn't
> make sense to me. Maybe you're trying to execute a multi-row insert, but
> that would be done with execute(), not executemany(), wouldn't it?
>
> Also, isn't "[(i,)[0] for i in hostlist]" exactly the same as "[i for i
> in hostlist]" which in turn is exactly the same as "hostlist"?
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

The OPs example has a formatted string, no idea what is in it...
My example creates a tuple out of each of the records you want to
insert and uses them in the bind variables.

You can do a loop through hostlist and do a single execute on each one
if you want.  It won't make a large impact.
The [(i,)[0] for i in hostlist] was mainly directed to you because
your structure ends up being a tuple inside a list which doesn't work
for cx_Oracle.  You need a straight tuple to bind to the statement.

My code creates a series of usable tuples for the executemany
function.
HTH,
Chris



More information about the Python-list mailing list