pythonsql-serverdjangofreetdsdjango-pyodbc

Django and django-pyodbc error NotImplementedError SQL Server v8 is not supported


I'm working with Django and PyODBC to connect to a SQL Server 2000 database and I'm gettinh this error:

Exception Type: NotImplementedError

Exception Value: SQL Server v8 is not supported.

My traceback:

Environment:


Request Method: POST
Request URL: http://localhost/sistemas/cadastro_paciente/

Django Version: 1.10.5
Python Version: 2.7.12
Installed Applications:
['admin_tools',
 'admin_tools.theming',
 'admin_tools.menu',
 'admin_tools.dashboard',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'cadastro',
 'fenotipo',
 'cuser']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'cuser.middleware.CuserMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/var/www/sistemas/cadastro/views.py" in cadastro_paciente
  1297.             if pacienteDB and pacienteHGP:

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in __nonzero__
  264.         return type(self).__bool__(self)

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in __bool__
  260.         self._fetch_all()

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _fetch_all
  1087.             self._result_cache = list(self.iterator())

File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in __iter__
  54.         results = compiler.execute_sql()

File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
  824.             sql, params = self.as_sql()

File "/usr/local/lib/python2.7/dist-packages/sql_server/pyodbc/compiler.py" in as_sql
  82.             supports_offset_clause = self.connection.sql_server_version >= 2012

File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in __get__
  35.         res = instance.__dict__[self.name] = self.func(instance)

File "/usr/local/lib/python2.7/dist-packages/sql_server/pyodbc/base.py" in sql_server_version
  390.         with self.temporary_connection() as cursor:

File "/usr/lib/python2.7/contextlib.py" in __enter__
  17.             return self.gen.next()

File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py" in temporary_connection
  564.         cursor = self.cursor()

File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py" in cursor
  231.             cursor = self.make_debug_cursor(self._cursor())

File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py" in _cursor
  204.         self.ensure_connection()

File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py" in ensure_connection
  199.                 self.connect()

File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py" in connect
  173.         self.init_connection_state()

File "/usr/local/lib/python2.7/dist-packages/sql_server/pyodbc/base.py" in init_connection_state
  349.         if self.sql_server_version < 2008:

File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in __get__
  35.         res = instance.__dict__[self.name] = self.func(instance)

File "/usr/local/lib/python2.7/dist-packages/sql_server/pyodbc/base.py" in sql_server_version
  395.                 raise NotImplementedError('SQL Server v%d is not supported.' % ver)

Exception Type: NotImplementedError at /cadastro_paciente/
Exception Value: SQL Server v8 is not supported.

My odbc.ini:

[HGP]
Driver = FREETDS
Server = "IP"
Port = 1433
Database = database
Driver = /usr/local/lib/libtdsodbc.so
UsageCount = 1

My freetds.conf

[HGP]
database = database
host = "IP"
port = 1433
tds version = 8.0

Driver configuration on odbcinst.int

[FREETDS]
Description = FreeTDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1

The my settings.py

'hgp': {
        'ENGINE': 'sql_server.pyodbc', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'hemovida',            # Or path to database file if using sqlite3.
        'USER': 'sa',                  # Not used with sqlite3.
        'PASSWORD': '',                # Not used with sqlite3.
        'HOST': 'IP',         # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',

        'OPTIONS': {
                'driver': 'FREETDS',
                'MARS_Connection': False,
                'dsn' : 'HGP',
            },
    },

My query

pacienteHGP = Paciente_Hemovida.objects.using('hgp').filter(paciente=postPaciente)

Solution

  • As mentioned before, using SQL Server 2000 is a really bad idea; it doesn't receive any support, bug fixes, or most importantly, security patches from Microsoft.

    That said, if you're using django-pyodbc-azure, you may be able to monkey patch it to get it working. See this line:

    https://github.com/michiya/django-pyodbc-azure/blob/azure-1.10/sql_server/pyodbc/base.py#L145

    Add 8: 2000, to the _sql_server_version dictionary:

    _sql_server_versions = {
        8: 2000,
        9: 2005,
        10: 2008,
        11: 2012,
        12: 2014,
        13: 2016,
        14: 2017,
    }
    

    I can place no guarantees on how this will or won't work, but you're welcome to try. That should get you past the error you're encountering.

    You will also want to modify your freetds.conf, as 8.0 is not a valid TDS Version:

    [HGP]
    database = database
    host = "IP"
    port = 1433
    tds version = 7.1
    

    Source: http://www.freetds.org/userguide/choosingtdsprotocol.htm

    Good luck, and again, I highly recommend upgrading to a modern version of SQL Server.