3

I am using pyodbc, ODBC with freetds to access a remote MSSQL database. I recently migrated from Centos + Python 2.4 to Ubuntu + Python 2.7 and the two are behaving differently. My freetds.conf is:

[global]
        # TDS protocol version
        tds version = 4.2

        # If you get out-of-memory errors, it may mean that your client
        # is trying to allocate a huge buffer for a TEXT field.  
        # Try setting 'text size' to a more reasonable limit 
        text size = 64512

[server]
        host = server
        port = 1333
        tds version = 8.0
        client charset = UTF-8

My Python code is (simplified):

import pyodbc
msConn =  pyodbc.connect('DSN=%s;UID=%s;PWD=%s' % (self.dsn, self.user, self.passwd))

msCur = msConn.cursor()
msQuery='''select ...'''
msCur.execute(msQuery % startUpdateStamp)

for row in msRows:
    print rows

When I execute the code above on my Centos server (with python 2.4), any special characters are encoded in utf-8. However when executing the same code with same configuration on the Ubuntu server (python 2.7), the special characters are in unicode. It is as if the configuration client charset = UTF-8 is being completely ignored.

However I've tried setting client charset to other values, in return the server does not allow the connection. Which means the client charset setting is getting through. I have also tried changing the tds version = 8.0 to no avail.

I've also looked here, but the answer does not explain why given the same database, two systems give two encodings.

How can I have the Ubuntu return UTF-8 as well? I'm not proficient at MSSQL or ODBC. Any help is greatly appreciated.

Community
  • 1
  • 1
bearwithme
  • 55
  • 7
  • Turns out on my Centos system, I was using a older version of pyodbc. The data is in fact in Unicode, but the older version incorrectly returns utf-8. (The full change log for pyodbc is [here](http://code.google.com/p/pyodbc/wiki/ReleaseNotes).) The old behaviour of returning utf-8 was fixed in a later version. The answer [here](http://stackoverflow.com/questions/16565028/pyodbc-remove-unicode-strings) was absolutely correct. – bearwithme Jun 27 '13 at 18:56

0 Answers0