I am trying to read data from SQL server into pandas data frame. 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)')
I had executed the function before and interrupted the execution with ctrl+k as I wanted to make a change in the function. 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?
I was facing the same issue. This was fixed when I used fetchall() function. 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. 
Hope this helps.
The issue is due to cursor being executed just before the pd.read_sql_query() command . Pandas is using the connection and SQL String to get the data . DB Cursor is not required .
#res = cur.execute(db_cmd)
sql_out = pd.read_sql_query(db_cmd, con, chunksize=10**6)
print(sql_out)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With