Is it possible to use several cursors for one connection with pyodbc and MS SQL Server?

I am using pyodbc on python 2.6 to connect to Microsoft SQL Server 2005. I open the connection, create a pair of cursors:

c1 = connection.cursor() c2 = connection.cursor() 

and then run the query for the first cursor.

 c1.execute("select * from foo") 

Now I run a query on the second cursor:

 c2.execute("select * from bar") 

... and I get an error: "The connection is busy with the results for another hstmt."

After doing c1.fetchall() or c1.close() I can use c2.

My question is: why am I even allowed to create multiple cursors in the connection, if I am allowed to use only one at a time, and the same can always be reused? And, if I want to run a query for each row of the results of another query, for example:

 for x in c1.execute(...): for y in c2.execute(...): 

Do I really need to create multiple connections to the same database?

+8
python sql-server pyodbc
source share
2 answers

This seems to be supported through multithreading: http://technet.microsoft.com/en-US/library/ms131700(v=sql.90).aspx

0
source share

In my own practice, I have never encountered the need to use more than one database cursor. Such problems are used to solve complex SQL-queries (joins, groups). Or (if you can ignore performance issues) using a few simple queries.

-one
source share

All Articles