虽然freetds.conf设置为charset UTF-8,但pyodbc fetchall在unicode中返回?

时间:2013-06-27 16:17:08

标签: odbc pyodbc freetds

我正在使用pyodbc,ODBC和freetds来访问远程MSSQL数据库。我最近从Centos + Python 2.4迁移到Ubuntu + Python 2.7,两者表现不同。我的freetds.conf是:

[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

我的Python代码是(简化的):

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

当我在Centos服务器上执行上面的代码(使用python 2.4)时,任何特殊字符都以utf-8编码。但是,当在Ubuntu服务器(python 2.7)上执行相同配置的相同代码时,特殊字符为unicode。就像配置client charset = UTF-8被完全忽略一样。

但是我尝试将client charset设置为其他值,作为回报,服务器不允许连接。这意味着客户端字符集设置正在通过。我也试过改变tds version = 8.0无济于事。

我也看了here,但答案并没有解释为什么给定相同的数据库,两个系统给出两个编码。

我怎样才能让Ubuntu返回UTF-8? 我不熟悉MSSQL或ODBC。非常感谢任何帮助。

0 个答案:

没有答案
相关问题