Cursors in a Loop

T

t_rectenwald

I have a python script that uses the cx_Oracle module. I have a list
of values that I iterate through via a for loop and then insert into
the database. This works okay, but I'm not sure whether I can use one
cursor for all inserts, and define it outside of the loop, or
instantiate and close the cursor within the loop itself. For example,
I have:

for i in hostlist:
cursor = connection.cursor()
sql= "insert into as_siebel_hosts_temp values('%s')" % (i)
cursor.execute(sql)
cursor.close()

And I've also tried:

cursor = connection.cursor()
for i in hostlist:
sql= "insert into as_siebel_hosts_temp values('%s')" % (i)
cursor.execute(sql)
cursor.close()

Both work fine, and execute in the same amount of time. I'm just
trying to understand what is the "correct" approach to use.

Thanks,
Tom
 
T

t_rectenwald

I have a python script that uses the cx_Oracle module.  I have a list
of values that I iterate through via a for loop and then insert into
the database.  This works okay, but I'm not sure whether I can use one
cursor for all inserts, and define it outside of the loop, or
instantiate and close the cursor within the loop itself.  For example,
I have:

for i in hostlist:
    cursor = connection.cursor()
    sql= "insert into as_siebel_hosts_temp values('%s')" % (i)
    cursor.execute(sql)
    cursor.close()

And I've also tried:

cursor = connection.cursor()
for i in hostlist:
    sql= "insert into as_siebel_hosts_temp values('%s')" % (i)
    cursor.execute(sql)
cursor.close()

Both work fine, and execute in the same amount of time.  I'm just
trying to understand what is the "correct" approach to use.

Thanks,
Tom

I think I have this one figured out. The answer would be the second
option, i.e. keep the cursor instantion and close outside of the
loop. I wasn't aware that one cursor could be used for multiple
executes.

Regards,
Tom
 
C

Carsten Haese

Actually, the correct approach would be "neither." You should NEVER use
string formatting to fill values into an SQL query. (Doing so causes
security vulnerabilities and performance problems. See, for example,
http://informixdb.blogspot.com/2007/07/filling-in-blanks.html for
detailed explanations.) Instead, you should use a parametrized query.

With a parametrized query, your code becomes this:

cursor = connection.cursor()
for i in hostlist:
cursor.execute("insert into as_siebel_hosts_temp values(?)", (i,) )
cursor.close()

Since this will save the database engine from having to re-parse the
query every time, it will run much faster if the list is long.

Even better would be to use executemany:

cursor = connection.cursor()
cursor.executemany("insert into as_siebel_hosts_temp values(?)",
[(i,) for i in hostlist] )
cursor.close()

Depending on whether cx_Oracle allows this, the list comprehension in
that example could be replaced by the generator expression
((i,) for i in hostlist), but I don't know if cx_Oracle allows
executemany with an arbitrary iterable.

Hope this helps,
 
C

Chris

Even better would be to use executemany:

cursor = connection.cursor()
cursor.executemany("insert into as_siebel_hosts_temp values(?)",
[(i,) for i in hostlist] )
cursor.close()

Depending on whether cx_Oracle allows this, the list comprehension in
that example could be replaced by the generator expression
((i,) for i in hostlist), but I don't know if cx_Oracle allows
executemany with an arbitrary iterable.

You should bind all variables to save the pool.

cursor = connection.cursor()
cursor.executemany("""insert into as_siebel_hosts_temp
values :)whole, :lot, :eek:f, :bind, :variables)
"""
,[(i,)[0] for i in hostlist]
)
connection.commit()
connection.close()
 
C

Carsten Haese

You should bind all variables to save the pool.

cursor = connection.cursor()
cursor.executemany("""insert into as_siebel_hosts_temp
values :)whole, :lot, :eek:f, :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"?
 
C

Chris

You should bind all variables to save the pool.
cursor = connection.cursor()
cursor.executemany("""insert into as_siebel_hosts_temp
values :)whole, :lot, :eek:f, :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"?

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top