_tds.InterfaceError when trying to connect to Azure Data Warehouse through Python 2.7 and ctds

6/17/2019

I'm trying to connect a python 2.7 script to Azure SQL Data Warehouse.

The coding part is done and the test cases work in our development environment. We're are coding in Python 2.7 in MacOS X and connecting to ADW via ctds. The problem appears when we deploy on our Azure Kubernetes pod (running Debian 9). When we try to instantiate a connection this way:

# init a connection
self._connection = ctds.connect(
    server='myserver.database.windows.net',
    port=1433,
    user="my_user@myserver.database.windows.net",
    timeout=1200,
    password="XXXXXXXX",
    database="my_db",
    autocommit=True
)

we get an exception that only prints the user name

my_user@myserver.database.windows.net

the type of the exception is

_tds.InterfaceError

The code deployed is the exact same and also the requirements are.

The documentation we found for this exception is almost non-existent.

Do you guys recognize it? Do you know how can we go around it?

We also tried in our old AWS instances of EC2 and AWS Kubernetes (which rans the same OS as the Azure ones) and it also doesn't work.

We managed to connect to ADW via sqlcmd, so that proves the pod can in fact connect (I guess).

EDIT: SOLVED. JUST CHANGED TO PYODBC

def connection(self):
    """:rtype: pyodbc.Connection"""
    if self._connection is None:

        env = '' # whichever way you have to identify it
        # init a connection

        driver = '/usr/local/lib/libmsodbcsql.17.dylib' if env == 'dev' else '{ODBC Driver 17 for SQL Server}' # my dev env is MacOS and my prod is Debian 9
        connection_string = 'Driver={driver};Server=tcp:{server},{port};Database={db};Uid={user};Pwd={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'.format(
            driver=driver,
            server='myserver.database.windows.net',
            port=1433,
            db='mydb',
            user='myuser@myserver',
            password='XXXXXXXXXXXX'
        )
        self._connection = pyodbc.connect(connection_string, autocommit=True)

    return self._connection
-- Alex Ingberg
azure-sql-database
azure-sqldw
freetds
kubernetes
python-2.7

1 Answer

6/17/2019

As Ron says, pyodbc is recommended because it enables you to use a Microsoft-supported ODBC Driver.

I'm going to go ahead and guess that ctds is failing on redirect, and you need to force your server into "proxy" mode. See: Azure SQL Connectivity Architecture

EG

# Get SQL Server ID
sqlserverid=$(az sql server show -n sql-server-name -g sql-server-group --query 'id' -o tsv)

# Set URI
id="$sqlserverid/connectionPolicies/Default"

# Get current connection policy
az resource show --ids $id

# Update connection policy
az resource update --ids $id --set properties.connectionType=Proxy
-- David Browne - Microsoft
Source: StackOverflow