开发者

execute sqlserver store procedure using python

I have this code

from win32com.client import Dispatch  

connection_string = "Provider=SQLNCLI;server=%s;initial catalog=%s;user id=%s;password=%s"%(server,db_name,user,pwd)  

dbConn = Dispatch("ADODB.Connection")  

dbConn.Open( connection_string )  

( rs, result ) = s.dbConn.Execute( query_string )

while not rs.EOF:
   for field in rs.Fields :
      dic[str( field.name )] = str( field.value )
开发者_C百科      print dic
      rs.MoveNext()

it works fine on 'select' , 'insert' and 'update' operations.

the code execute the store procedures but it close the record set before the while statment.

here is the error:

... pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'ADODB.Recordset', u'Operation is not allowed when the object is closed.' ...


Not sure if this will help, but here's how to do it with pyODBC:

        cn = pyodbc.connect("DSN=myDBName")
        cn.autocommit = True
        cr = cn.cursor()
        cr.execute("set nocount on")
        cr.execute("exec myStoredProc")
        ...
        cr.close()
        cn.close()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜