Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

python - Can I use multiple cursors on one connection with pyodbc and MS SQL Server?

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")

...and I get an error: "Connection is busy with results for another hstmt."

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to this guy

Cursor objects are used to execute SQL statements. ODBC and pyodbc allow multiple cursors per connection, but not all databases support this.

and you can determine concurrent cursors can be supported with:

import pyodbc
connection = pyodbc.connect(...)
how_many = connection.getinfo(pyodbc.SQL_MAX_CONCURRENT_ACTIVITIES)
print(how_many)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...