我可以在与 pyodbc 和 MS SQL Server 的一个连接上使用多个游标吗? [英] Can I use multiple cursors on one connection with pyodbc and MS SQL Server?
问题描述
我在 python 2.6 上使用 pyodbc 连接到 Microsoft SQL Server 2005.我打开一个连接,创建几个游标:
I'm using pyodbc on python 2.6 to connect to Microsoft SQL Server 2005. I open a connection, create a couple of cursors:
c1 = connection.cursor()
c2 = connection.cursor()
然后在第一个游标上运行查询.
and then run a query on the first cursor.
c1.execute("select * from foo")
现在我在第二个游标上运行查询:
Now I run a query on the second cursor:
c2.execute("select * from bar")
...我收到一个错误:连接正忙于另一个 hstmt 的结果."
...and I get an error: "Connection is busy with results for another hstmt."
在我执行了 c1.fetchall()
或 c1.close()
之后,我就可以使用 c2.
After I do a c1.fetchall()
or c1.close()
then I can use c2.
我的问题是:为什么我什至允许在一个连接上创建多个游标,如果我一次只允许使用一个,并且始终可以重复使用同一个游标?而且,如果我想对另一个查询结果的每一行运行一个查询,如下所示:
My question is: Why am I even allowed to create multiple cursors on a connection, if I'm only allowed to use one at a time, and the same one can always be reused? And, if I want to run a query for each row of the results of another query, like this:
for x in c1.execute(...):
for y in c2.execute(...):
我真的必须创建到同一个数据库的多个连接吗?
do I really have to create multiple connections to the same database?
推荐答案
这似乎是通过多线程支持的:http://technet.microsoft.com/en-US/library/ms131700(v=sql.90).aspx
This appears to be supported via multithreading: http://technet.microsoft.com/en-US/library/ms131700(v=sql.90).aspx
这篇关于我可以在与 pyodbc 和 MS SQL Server 的一个连接上使用多个游标吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!