Python和sqlserver存储过程挂起

2024-04-23 14:41:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让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

Tags: 文件代码服务器内容sqlserveros过程
1条回答
网友
1楼 · 发布于 2024-04-23 14:41:20

在搜索之后,脚本停止将记录发布到表中,我找到了问题的解决方案。我需要在脚本中添加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()

相关问题 更多 >