[英]DatabaseError: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0) (SQLExecDirectW)')
I am trying to read data from SQL server into pandas data frame.我正在尝试将数据从 SQL 服务器读取到 Pandas 数据框中。 Below is the code.
下面是代码。
def get_data(size):
con = pyodbc.connect(r'driver={SQL Server}; server=SPROD_RPT01; database=Reporting')
cur = con.cursor()
db_cmd = "select distinct top %s * from dbo.KrishAnalyticsAllCalls" %size
res = cur.execute(db_cmd)
sql_out = pd.read_sql_query(db_cmd, con, chunksize=10**6)
frames = [chunk for chunk in sql_out]
df_sql = pd.concat(frames)
return df_sql
df = get_data(5000000)
I am getting following error:我收到以下错误:
pandas.io.sql.DatabaseError: Execution failed on sql 'select distinct top 500000 * from dbo.KrishAnalyticsAllCalls': ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0) (SQLExecDirectW)')
pandas.io.sql.DatabaseError: sql 'select distinct top 500000 * from dbo.KrishAnalyticsAllCalls' 上的执行失败: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0) (SQLExecDirectW)')
I had executed the function before and interrupted the execution with ctrl+k
as I wanted to make a change in the function.我之前已经执行过这个函数,并用
ctrl+k
中断了执行,因为我想对函数进行更改。 Now, after making the change when I'm trying to execute the function I am getting the above error.现在,当我尝试执行该函数时进行更改后,我收到了上述错误。
How can I kill that connection/IPython Kernel since I don't know of any IPython Kernel running executing the query in the function?由于我不知道在函数中执行查询的任何 IPython 内核,我该如何终止该连接/IPython 内核?
I was facing the same issue.我面临着同样的问题。 This was fixed when I used
fetchall()
function.当我使用
fetchall()
函数时,这是固定的。 The following the code that I used.以下是我使用的代码。
import pypyodbc as pyodbc
def connect(self, query):
con = pyodbc.connect(self.CONNECTION_STRING)
cursor = con.cursor()
print('Connection to db successful')
cmd = (query)
results = cursor.execute(cmd).fetchall()
df = pd.read_sql(query, con)
return df, results
Using cursor.execute(cmd).fetchall()
instead of cursor.execute(cmd)
resolved it.使用
cursor.execute(cmd).fetchall()
而不是cursor.execute(cmd)
解决了它。 Hope this helps.希望这可以帮助。
The issue is due to cursor being executed just before the pd.read_sql_query() command .问题是由于游标在 pd.read_sql_query() 命令之前执行。 Pandas is using the connection and SQL String to get the data .
Pandas 使用连接和 SQL 字符串来获取数据。 DB Cursor is not required .
不需要 DB Cursor。
#res = cur.execute(db_cmd)
sql_out = pd.read_sql_query(db_cmd, con, chunksize=10**6)
print(sql_out)
Most likely you haven't connected to the SQL server yet.您很可能还没有连接到 SQL 服务器。 Or, you connected in a previous instance for a different SQL query that was run.
或者,您在之前的实例中为运行的不同 SQL 查询进行了连接。 Either way, you need to re-establish the connection.
无论哪种方式,您都需要重新建立连接。
import pyodbc as pyodbc
conn = pyodbc.connect('Driver={YOUR_DRIVER};''Server=YOUR_SERVER;''Database=YOUR_DATABASE;''Trusted_Connection=yes')
Then execute your SQL:然后执行你的 SQL:
sql = conn.cursor()
sql.execute("""ENTER YOUR SQL""")
Then transform into Pandas:然后转化为 Pandas:
df = pd.DataFrame.from_records(sql.fetchall(),columns=[desc[0] for desc in sql.description])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.