我正在Windows 10上测试我的代码。 我有一个 Django 应用程序,需要在远程 SQL Server 数据库上调用存储过程。 以下是 settings.py 中的数据库代码段:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db1',
'HOST': 'mycompany.com',
'PORT': '3306',
'USER': 'user',
'PASSWORD': 'pw',
},
'ss': {
'ENGINE': 'django_pyodbc',
'NAME': 'db2',
'HOST': 'myserverSQLEXPRESS',
'USER': 'myuser',
'PASSWORD': 'mypw',
'PORT': '1433',
# 'DRIVER': 'SQL Server',
'OPTIONS': {
'driver_supports_utf8': True,
'host_is_server': True, # must be True for remote db
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=8.0',
},
},
}
这是我视图中的代码片段:
cursor = connections['ss'].cursor()
cursor.execute("{call dbo.mysproc(?)}", (id))
当我执行 cursor.execute 语句时,出现此错误:
django.db.utils.DatabaseError: ('SQL 包含 1 个参数标记, 但提供了 36 个参数","HY000"(
我的参数 id 是一个 GUID。思潮?
这是修复,只需将参数周围的括号更改为方括号:
cursor.execute("{call dbo.mysproc(?)}", [id])
我通过反复试验发现了这一点。