这里列出了哪些有效,哪些无效,以及错误。 尝试发送十进制/货币数据类型变量时。我不确定问题是否出在不同的数据类型上,或者传入的参数超过 3 个。前 2 个参数看起来像数字,但它们以字符串形式发送。 数据库是SQL Server 2017
我注意到参数的数据类型是根据模型设置的 - 这会改变什么吗? param1 - '10',param2 是 '-100'和 param3: 十进制('100'(
# WORKS - there's a charfield/nvarchar datatype, one param only (pyodbc/mssql)
# cursor.execute('EXEC [dbo].[sproc2] @charfield-param={}'.format(stringValue))
# DOESNT WORK
# cursor.execute('EXEC [dbo].[sproc1] @charfield-param1=%s @charfield-param2=%s @money-decimal-param=%d', (charfield-param1, charfield-param2, 100000))
# %d format: a number is required, not str
# cursor.execute('EXEC [dbo].[sproc1] @charfield-param1=%s @charfield-param2=%s @money-decimal-param=%s', (charfield-param1, charfield-param2, 100000))
# ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '@charfield-param2'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)")
# cursor.execute('EXEC [dbo].[sproc1] @charfield-param1={} @charfield-param2={} @money-decimal-param={}'.format(charfield-param1, charfield-param2, 100000))
# ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '@charfield-param2'. (102) (SQLExecDirectW)")
# ignored_wrapper_args
# (False,
# {'connection': <sql_server.pyodbc.base.DatabaseWrapper object at 0x04B21470>,
# 'cursor': <django.db.backends.utils.CursorDebugWrapper object at 0x04CEDD10>})
# params
# None
# self
# <django.db.backends.utils.CursorDebugWrapper object at 0x04CEDD10>
# sql
# ('EXEC [dbo].[sproc1] @charfield-param1=10 @charfield-param2=-100 '
# '@money-decimal-param=100000')
# DOESNT WORK
# cursor.execute('EXEC [dbo].[sproc1] @charfield-param1=%s @charfield-param2=%s @money-decimal-param=%s', (charfield-param1, charfield-param2, 100000))
# # ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '@charfield-param2'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)")
# cursor.execute('EXEC [dbo].[sproc1] @charfield-param1={} @charfield-param2={} @money-decimal-param={}'.format(charfield-param1, charfield-param2, money-decimal-param3))
# ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '@charfield-param2'. (102) (SQLExecDirectW)")
# params
# ()
# self
# <sql_server.pyodbc.base.CursorWrapper object at 0x03FD46B0>
# sql
# ('EXEC [dbo].[sproc1] @charfield-param1=10 @charfield-param2=-100 '
# '@money-decimal-param=200000')
# cursor.execute('EXEC [dbo].[sproc1] @charfield-param1={} @charfield-param2={} @money-decimal-param={}'.format(charfield-param1, charfield-param2).format(money-decimal-param3))
# ignored_wrapper_args
# (False,
# {'connection': <sql_server.pyodbc.base.DatabaseWrapper object at 0x04019470>,
# 'cursor': <django.db.backends.utils.CursorDebugWrapper object at 0x04019790>})
# params
# None
# self
# <django.db.backends.utils.CursorDebugWrapper object at 0x04019790>
# sql
# ('EXEC [dbo].[sproc1] @charfield-param1=10 @charfield-param2=-100 '
# '@money-decimal-param=200000')
cursor.execute('EXEC [dbo].[sproc1] @charfield-param1={} @charfield-param2={} @money-decimal-param={}', (str(charfield-param1), str(charfield-param2), int(money-decimal-param3)))
# not all arguments converted during string formatting
# cursor.execute('EXEC [dbo].[sproc1] @charfield-param1={} @charfield-param2={} @money-decimal-param={}', (str(charfield-param1), str(charfield-param2), int(100000)))
# Error: '<' not supported between instances of 'NoneType' and 'int'
将存储的 proc 签名更改为预期 3 个字符,而不是 2 个和一个金钱字段,并不能解决我的问题。所以,看起来像一个格式问题?我对 Python 和 django 很陌生,所以这可能是一个非常容易修复的问题!
cursor.execute('EXEC [dbo].[sproc1] @charfield-param1={} @charfield-param2={} @charfield-param3={}'.format(str(charfield-param1), str(charfield-param2), charfield-param3))
cursor.execute('EXEC [dbo].[sproc1] @charfield-param1={} @charfield-param2={} @charfield-param2={}'.format(charfield1, charfield2).format(charfield3))
IndexError: tuple index out of range
编辑:返回一个整数,以及来自不同表的不同命名列(不一定映射到映射的表/模型(。
错误详细信息:
cursor.execute('EXEC [dbo].[stored_proc] @Param1={} @Param2={} @NewParam3={}'.format(strparam1, strparam2,strparam3))
环境: 本地变量:
ignored_wrapper_args
(False,
{'connection': <sql_server.pyodbc.base.DatabaseWrapper object at 0x05E96490>,
'cursor': <django.db.backends.utils.CursorDebugWrapper object at 0x060D51B0>})
params: None
self : <django.db.backends.utils.CursorDebugWrapper object at 0x060D51B0>
sql: ('EXEC [dbo].[stored_proc] @Param1=10 '
'@Param2=-100 @NewParam3=100000')
Request Method: POST
Request URL: http://localhost:8000/app-url/
Django Version: 2.1.1
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_name']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
追踪:
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangodbbackendsutils.py" in _execute
83. return self.cursor.execute(sql)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagessql_serverpyodbcbase.py" in execute
546. return self.cursor.execute(sql, params)
The above exception (('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '@Param2'. (102) (SQLExecDirectW)")) was the direct cause of the following exception:
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangoviewsgenericbase.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangoviewsgenericbase.py" in dispatch
88. return handler(request, *args, **kwargs)
File "C:Githubproject-nameapp_nameviews.py" in post
90. results = ModelName.get_stored_proc_data(str(param1), str(param2), decimalParam3)
File "C:Githubproject-nameapp_namemodels.py" in get_stored_proc_data
89. cursor.execute('EXEC [dbo].[stored_proc] @Param1={} @Param2={} @NewParam3={}'.format(param1, param2,decimalParam3))
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangodbbackendsutils.py" in execute
100. return super().execute(sql, params)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangodbbackendsutils.py" in execute
68. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangodbbackendsutils.py" in _execute_with_wrappers
77. return executor(sql, params, many, context)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangodbbackendsutils.py" in _execute
85. return self.cursor.execute(sql, params)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangodbutils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagesdjangodbbackendsutils.py" in _execute
83. return self.cursor.execute(sql)
File "C:Usersuser_name.virtualenvsprioject_namelibsite-packagessql_serverpyodbcbase.py" in execute
546. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /app-url/class/
Exception Value: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '@Param2'. (102) (SQLExecDirectW)")
我有一些时间来测试这个,我相信你已经在django-pyodbc-azure
中发现了一个错误。我已经找到了一个解决方法,但它不是最漂亮的。
如果打开与具有相同凭据的pyodbc
的单独连接,则可以执行以下操作:
import pyodbc
con = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=yourserver.com;PORT=1433;DATABASE=yourdb;UID=yourdb_user;PWD=password;')
cursor = con.cursor()
cursor.execute('EXEC usp_get_user_data @user_name = ?, @user_type = ?', 'flipperpa', 'admin')
rows = cursor.fetchall()
for row in rows:
print(row)
祝你好运!
你可以用这种方式调用带有参数的存储过程
from django.db import connection
cursor = connection.cursor()
cursor.callproc('test_procedure', [first_type, second_type])
如果您使用字典列表获取数据,那么您也可以将其用于额外信息
list_obj = [ dict( zip( [column[0] for column in cursor.description] , record ) ) for record in cursor.fetchall()]