Gurmeet Kaur
Gurmeet Kaur

Reputation: 55

Not able to connect to SQL Server 2012 from current version of DJango and Python

I am trying to connect to SQL server 2012 using Pyodbc and Django framework.

But the version of Django is 2.1. Now, when I try to connect using below code when I only have Pyodbc installed then the below code doesnot run.

DATABASES = {
    'default': {
        # 'ENGINE': '{ODBC Driver 11 for SQL Server}', Tried this as well but it did not work.
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'MY_DATABASE',
        # 'HOST': 'XXX.XX.XXX.XXX', Tried this as well but it did not work.
        'SERVER': 'XXX.XX.XXX.XXX',
        # 'HOST': 'SQLSERVER_InstanceName',
        'PORT': '',
        'USER': 'SQLSERVERUSER',
        'PASSWORD': 'USERs PASSWORD',
        'DATABASE': 'MY_DATABASE',
        # 'Trusted_Connection': 'Yes',
        # 'OPTIONS': {
        #             'driver': 'ODBC Driver 11 for SQL Server',
        #            }
    },
}

But when I install django-pyodbc-azure and try to run the above code by running python manage.py inspectdb command, it works. But the problem with using django-pyodbc-azure is that it downgrades the current version of django to 2.0.8 - which I do not want.

I do not want to use FreeTDS or Pymssql; I want to go with pyodbc only.

Can someone please suggest some way for this?

Upvotes: 0

Views: 516

Answers (1)

Felix D.
Felix D.

Reputation: 133

As of today, django-pyodbc-azure supports Django 2.1. Below is my configuration.

python virtual environment for django-odbc-azure

And here is the code that I used to configure the connection.

DATABASES = {
'default': {
    'ENGINE': "sql_server.pyodbc",
    'HOST': "server_name",
    'USER': "username",
    'PASSWORD': "password",
    'NAME': "database_name",
    'OPTIONS' : {
        'driver': 'ODBC Driver 13 for SQL Server',
    },
  }
}

Note: Don't use the IP address of the server as the HOST, instead use the computer name.

Upvotes: 1

Related Questions