djangostored-proceduresdjango-pyodbc

django-pyodbc and calling a stored procedure


I'm testing my code on Windows 10. I have a Django application that needs to call a stored procedure on a remote SQL Server database. Here's the DATABASES snippet from 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',
    },
},

}

Here's a code snippet from my view:

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

When I execute the cursor.execute statement I get this error:

django.db.utils.DatabaseError: ('The SQL contains 1 parameter markers, but 36 parameters were supplied', 'HY000')

My parameter, id, is a GUID. Thoughts?


Solution

  • Here's the fix, simply changed the parentheses surrounding the parameter to square brackets:

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

    I found this by trial and error.