简体   繁体   English

DatabaseError: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0) (SQLExecDirectW)')

[英]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.

相关问题 'HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无法打开登录请求的服务器“dominio”。 登录失败 - 'HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open server "dominio" requested by the login. The login failed mysql.connector.errors.DatabaseError: 2003 (HY000): 无法连接到“127.0.0.1”上的 MySQL 服务器 (111) - mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111) mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL 服务器主机 'db' (2) - mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL server host 'db' (2) 将 panda df 移至 teradata 表:[HY000] [Teradata][ODBC Teradata Driver][Teradata Database] 时间戳无效 - Move panda df to teradata table: [HY000] [Teradata][ODBC Teradata Driver][Teradata Database] Invalid timestamp pyodbc.Error:('HY000','驱动程序未提供错误!') - pyodbc.Error: ('HY000', 'The driver did not supply an error!') pyodbc MERGE INTO 错误:HY000:驱动程序未提供错误 - pyodbc MERGE INTO error: HY000: The driver did not supply an error pyodbc.Error:('HY000','[HY000][Microsoft][ODBCDriver18forSQL Server]SSPIProvider:NoKerberoscredentialsavailable(默认缓存:FILE:/tmp/krb5cc_1051) - pyodbc.Error:('HY000','[HY000][Microsoft][ODBCDriver18forSQL Server]SSPIProvider:NoKerberoscredentialsavailable(default cache: FILE:/tmp/krb5cc_1051) mysql.connector.errors.DatabaseError:2003(HY000):无法连接到“本地主机”(111)上的 MySQL 服务器。 Flask 应用程序 - mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'localhost' (111). Flask app (Python,MySQL)数据库错误:2003(HY000):无法连接到“196.xxx.xxx.xxx:3306”上的 MySQL 服务器(113) - (Pythin , MySQL) DatabaseError: 2003 (HY000): Can't connect to MySQL server on '196.xxx.xxx.xxx:3306' (113) Python SQL 错误:2003 (HY000):无法连接到“192.168.1.1:3306”(10061) SSMS 上的 MySQL 服务器 - Python SQL Error: 2003 (HY000): Can't connect to MySQL server on '192.168.1.1:3306' (10061) SSMS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM