[freetds] Force use of UTF-8 instead of ISO8859-1

Michael Mata michaelvmata at gmail.com
Wed Jan 27 17:13:22 EST 2010


I'm running CentOS 5.3, freetds-patched (0.82), and pyodbc 2.1.6, which I
use to connect to SQL Server 2005.  My freetds.conf file:

[global]
        tds version = 8.0
        client charset = UTF-8
        text size = 32768

I can run most queries using pyodbc fine, but I'm running into a problem
with unicode characters.  When I select from an nvarchar column, the
returned value is an ascii string instead of unicode.  When I run a insert
query into a nvarchar column with a unicode parameter, I get the following
exception:

Error: ('HY004', '[HY004] [FreeTDS][SQL Server]Invalid data type (0)
(SQLBindParameter)')

In the freetds.log file, I noticed the following lines:

log.c:190:Starting log file for FreeTDS 0.82.1.dev.20091223
        on 2010-01-27 06:45:14 with debug flags 0x4fff.
iconv.c:197:names for ISO-8859-1: ISO-8859-1
iconv.c:197:names for UTF-8: UTF-8
iconv.c:197:names for UCS-2LE: UCS-2LE
iconv.c:197:names for UCS-2BE: UCS-2BE
iconv.c:363:iconv to convert client-side data to the "ISO-8859-1" character
set
iconv.c:516:tds_iconv_info_init: converting "ISO-8859-1"->"UCS-2LE"
iconv.c:516:tds_iconv_info_init: converting "ISO-8859-1"->"UCS-2LE"
net.c:210:Connecting to X.X.X.X port 1435 (TDS version 8.0)

I might be mistaken, but it seems like the client side data is ASCII
(ISO-8859-1), instead of the client charset value of UTF-8.  In the odbc.log
file, the only relevant lines I could find seem to confirm this theory:

[ODBC][29390][SQLDriverConnectW.c][246]
                Entry:
                        Connection = 0x8466c20
                        Window Hdl = (nil)
                        Str In = [DRIVER={SQL Native
Client};SERVER=******;PORT=1435;DATABASE=******;UID=******;PWD=******][length
= 108\
 (SQL_NTS)]
                        Str Out = (nil)
                        Str Out Max = 0
                        Str Out Ptr = (nil)
                        Completion = 0
                UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE
'UCS-2LE'

How do I force Free-TDS to accept and return UTF-8 characters?



More information about the FreeTDS mailing list