pythonsql-serverpyodbcfreetds

How do I update FreeTDS driver in RHEL-like Docker image?


I am passing some emoticon data from a postgres database object to SQL Server 2016 using pyodbc.

Line 5 has my freeTDS driver I'm using.

def __insert_records(self, rows, target_fields):
        try:      
            mssql_connection = BaseHook.get_connection(self.mssql_conn_id)

            connection = pyodbc.connect(DRIVER='FreeTDS',host=mssql_connection.host,DATABASE=mssql_connection.schema,user=mssql_connection.login,password=mssql_connection.password,ClientCharset='utf-8',port=mssql_connection.port,driver='/usr/lib64/libtdsodbc.so')
            cursor = connection.cursor()

            for i, row in enumerate(rows, 1):
                record = []

                for cell in row:
                    record.append(self._serialize_cell(cell))

                record_dictionary = self.__get_record_dictionary(record, target_fields)
               cursor.execute(self.__generate_insert_sql_statement(record_dictionary))

            connection.commit()

            cursor.close()
            connection.close()
        except pyodbc.ProgrammingError as programmingError:
            sqlstate = programmingError.args[0]
            if sqlstate = '42000':
                print(programmingError.args[0])

I need to update the driver (freeTDS) so that I can get around a bug when inserting emojis (related: https://github.com/FreeTDS/freetds/issues/317).

UPDATE: After accessing the CLI in my docker image, I ran tsql -C to get my compile time settings:

Version: freetds v1.1.20

Solution

  • So apparently your Dockerfile already has the required commands to install pyodbc. Therefore your container should already have

    Now instead of installing freetds from EPEL (or wherever) your Dockerfile will need to perform the following (which uses the latest stable version of FreeTDS at the time of writing):

    curl https://www.freetds.org/files/stable/freetds-1.2.18.tar.gz > freetds-1.2.18.tar.gz
    
    tar -xvzf freetds-1.2.18.tar.gz
    
    cd freetds-1.2.18/
    
    ./configure
    
    make
    
    sudo make install
    
    echo "" | sudo tee -a /etc/odbcinst.ini > /dev/null
    
    echo "[FreeTDS_1.2.18]" | sudo tee -a /etc/odbcinst.ini > /dev/null
    
    echo "Driver=/usr/local/lib/libtdsodbc.so" | sudo tee -a /etc/odbcinst.ini > /dev/null
    

    Then you should be able to create a pyodbc connection like so:

    >>> import pyodbc
    >>> cnxn = pyodbc.connect("DRIVER=FreeTDS_1.2.18;SERVER=192.168.0.179;PORT=49242;UID=sa;PWD=_whatever_;")
    >>> print(cnxn.execute("SELECT @@VERSION").fetchval())
    Microsoft SQL Server 2017 (RTM-GDR) (KB4583456) - 14.0.2037.2 (X64) 
        Nov  2 2020 19:19:59 
        Copyright (C) 2017 Microsoft Corporation
        Express Edition (64-bit) on Windows 8.1 Pro 6.3 <X64> (Build 9600: )
    
    >>> print(cnxn.getinfo(pyodbc.SQL_DRIVER_NAME))
    libtdsodbc.so
    >>> print(cnxn.getinfo(pyodbc.SQL_DRIVER_VER))
    01.02.0018