[我正在尝试让Python在我的SQL Server中运行存储过程,这启动了一系列过程,其中涉及导入处理文件并输出几个文件的过程。
到目前为止,我已经有了我的代码,因此它可以接受表的输入,但是当它调用存储过程时,整个过程都将挂起。
在服务器上检查它正在等待Who2
的服务器上正在检查的preemptive_OS_Pipeops
,搜索表明它正在等待SQL Server外部的某些内容来完成操作,然后继续。
如果可以使用pyodbc盲目激活存储过程然后关闭连接,是否有人可以阐明?
我的信念是,仅告诉程序运行,然后将其关闭即可解决问题,但我在为此找到代码时遇到了问题
Python代码:
connection2 = pypyodbc.connect('Driver={SQL Server}; Server=server;Database=db', timeout=1)
cursor2 = connection2.cursor()
cursor2.execute("{CALL uspGoBabyGo}")
connection2.close()
return 'file uploaded successfully'
存储过程:
BEGIN
SET NOCOUNT ON;
EXECUTE [dbo].[uspCableMapImport]
END
搜索之后,脚本停止了将记录发布到表中,我找到了解决问题的方法。我需要在脚本中添加autocommit = True行,现在代码如下;
connection = pyodbc.connect('Driver={SQL Server};
Server='Server';Database='DB';Trusted_Connection=yes')
connection.autocommit=True
cursor = connection.cursor()
referee = file.filename.rsplit('.')[0]
SQLCommand = ("INSERT INTO RequestTable(Reference, Requested) VALUES ('"+ str(referee) +"', " + datetime.now().strftime('%Y-%m-%d') + ")")
cursor.execute(SQLCommand)
connection.commit
SQLCommand2 = ("{CALL uspGoBabyGo}")
cursor.execute(SQLCommand2)
connection.commit
connection.close()