'error :- ProgrammingError: Attempt to use a closed connection

hi all im trying to run a stored procedure using python however im getting an error below is my code

import pyodbc
import sqlalchemy
from urllib.parse import quote_plus
import sqlalchemy as sa

driver='SQL Server Native Client 11.0'
params = quote_plus(r'DRIVER={SQL Server Native Client 11.0};'
r'SERVER=i cant disclose my server ;'
r'DATABASE=ForTestPurpose;'
r'Uid='+sql_userid+';'
r'Pwd='+sql_pwd+';')
engine = sqlalchemy.create_engine('mssql+pyodbc:///?odbc_connect=%s' % params)
query= sa.text("EXEC InsertTestVisionOpenCases '"+yesterday1+"' ")

engine.execute(query)#you can try entering with parameters as how you give in your sql 
engine.execution_options(autocommit=True)

cursor.close()
conn.close()

and im getting this error

ProgrammingError: Attempt to use a closed cursor.

please let me know how can i remove that error



Solution 1:[1]

I was able to solve the issue please check the below code it helped me to move ahead.

conn = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};'
r'SERVER=W-MUM-myserver_name;'
r'DATABASE=Analytics;'
r'Uid='+sql_userid+';'
r'Pwd='+sql_pwd+';')
cursor = conn.cursor()
params=date(yesterday.year,yesterday.month,yesterday.day)
storedProc = "Exec ForTestPurpose.dbo.InsertThreeVisionOpenCases @parameter1 = ?"
cursor.execute( storedProc, params )
conn.commit()
cursor.close()
conn.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rohan tambe