MySQLdb and Cursor

Glauco glauco at sferacarta.com
Thu Oct 9 15:29:59 CEST 2003


Michel Combe wrote:

> Hi all,
> 
> I'm writing a program that will read an ASCII file periodically and update
> several tables in a MySQL database.
> My question is "Can I use the same cursor for several SQL requests (SELECT
> and INSERT) or do I have to close the cursor between 2 requests ?".
> 
> Regards
> Michel Combe
> 
> 

best way is to create a connection ar the start of one session work. use 
this connection to do an entire transaction. At the end of work you must 
commit or rollback. But this don't mean that you can misc select and 
insert on tha same cursor.

con = MySql.connection....
cur1 = con.cursor()
cur1.execute("select.....
for rec in cur1:
     do something
     cur2 = con.cursor()
     cur2.execute("insert ....

conn.commit or rollback
conn.close()

In this  stupid example you CANNOT use only cur1 !!
Is not necessary to close cursors , last line do this for you


Bye
Glauco






More information about the Python-list mailing list