2
我有一个问题,pyodbc不捕获存储过程返回的错误。我的实际存储过程做了很多事情,但为了演示错误,我创建了一个简单的proc和关联的python代码。相关代码如下:pyodbc不报告sql服务器错误
存储过程:
CREATE PROCEDURE [dbo].[testErrors]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into Parameter(Name, Units, DataType) values (null, null, null)
END
GO
Python代码:
import pyodbc
connectString = "DRIVER={SQL Server};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s" % (r'srvr', r'mydb', r'usr', r'pwd')
cnxn = pyodbc.connect(connectString)
cnxn.autocommit = True
cursor = cnxn.cursor()
cursor.execute("exec testErrors")
cursor.close()
错误,我得到预期:
cursor.execute("exec testErrors")
pyodbc.IntegrityError: ('23000', "[23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert the value NULL into column 'Name', table 'Parametrics.dbo.Parameter'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW); [01000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated. (3621)")
但是,当我修改存储过程以包含select语句时,错误不再返回到我的python代码中。如果没有错误发生,Python代码就会退出。
更新存储过程:
CREATE PROCEDURE [dbo].[testErrors]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select 108
-- Insert statements for procedure here
insert into Parameter(Name, Units, DataType) values (null, null, null)
END
GO
看来pyodbc不会看到错误时,有通过SELECT语句交织结果记录。这里有什么问题,有没有办法解决这个问题?
编辑:我使用ADO试图与pymssql和看到相同的行为
那么有pymssql – 2015-03-31 22:16:21
@NickBailey尝试,和相同的行为 – user3885927 2015-03-31 22:19:06