使用 django-mssql 调用存储过程

Calling a Stored Procedure using django-mssql

我正在尝试调用我正在使用的多数据库项目的存储过程。我有 2 个存储过程在工作,但是这个特定的存储过程不工作并且 returns 一个错误:

Traceback (most recent call last):
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\rest_framework\views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\rest_framework\views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\rest_framework\views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "d:\Projects\Django\HRSys\hrs\hrs_api\views.py", line 79, in post
result_set = cursor.fetchall()
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\db\utils.py", line 101, in inner
return func(*args, **kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\django\db\utils.py", line 101, in inner
return func(*args, **kwargs)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\sqlserver_ado\dbapi.py", line 719, in fetchall
return self._fetch()
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\sqlserver_ado\dbapi.py", line 671, in _fetch
self._raiseCursorError(FetchFailedError, 'Attempting to fetch from a closed connection or empty record set')
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\sqlserver_ado\dbapi.py", line 488, in _raiseCursorError
eh(self.connection, self, errorclass, errorvalue)
File "d:\Projects\Django\AAAVirtualEnv\lib\site-packages\sqlserver_ado\dbapi.py", line 103, in standardErrorHandler
raise errorclass(errorvalue)
django.db.utils.Error: Attempting to fetch from a closed connection or empty record set

我是这样调用存储过程的:

cursor = connections['backend'].cursor()
    try:
        cursor.callproc('[dbo].[GetLogonControlJSON]', ['10011', 'encrypted', '127.0.0.1'])
        result_set = cursor.fetchall()
    finally:
        cursor.close()

我也这样做了,但我仍然得到同样的错误:

cursor = connections['backend'].cursor()
try:
    cursor.execute('EXEC [dbo].[GetLogonControlJSON] @LogonID=%s, @Password=%s, @IP=%s' % ('10011', 'encrypted', '127.0.0.1'))
    result_set = cursor.fetchall()
finally:
    cursor.close()

请帮帮我。 :)

谢谢!

在这种情况下,您应该使用 execute() 方法而不是 callproc()

错误信息告诉你一切:

Attempting to fetch from a closed connection or empty record set

查看文档(第二个示例):Stored Procedures