django-pyodbc 并调用存储过程

问题描述 投票:0回答:2

我正在 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': 'myserver\SQLEXPRESS',
    '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。 想法?

django stored-procedures django-pyodbc
2个回答
3
投票

解决方法如下,只需将参数周围的括号更改为方括号即可:

cursor.execute("{call dbo.mysproc(?)}", [id])

我通过反复试验发现了这一点。


0
投票

Tengo el Mismo Problema me da error y error estoy siguiendo un curso y ahí se hace con mysql pero yo enlace mi base de datos con swlserver no se si hay alguna diffencia en ejecutar el 光标.执行

观看有关 mysql 的视频: 光标.execute("调用 myprocedure()") Debo cambiar 与 las llaves 一起使用 sqlserver 吗?

© www.soinside.com 2019 - 2024. All rights reserved.