using pyodbc on linux to insert unicode or utf-8 chars in a nvarchar mssql field

前端 未结 4 1142
抹茶落季
抹茶落季 2020-12-05 04:47

I am using Ubuntu 9.04

I have installed the following package versions:

unixodbc and unixodbc-dev: 2.2.11-16build3
tdsodbc: 0.82-4
l         


        
4条回答
  •  攒了一身酷
    2020-12-05 05:40

    I use UCS-2 to interact with SQL Server, not UTF-8.

    Correction: I changed the .freetds.conf entry so that the client uses UTF-8

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

    Now, bind values work fine for UTF-8 encoded strings. The driver converts transparently between the UCS-2 used for storage on the dataserver side and the UTF-8 encoded strings given to/taken from the client.

    This is with pyodbc 2.0 on Solaris 10 running Python 2.5 and FreeTDS freetds-0.82.1.dev.20081111 and SQL Server 2008

    
    import pyodbc
    test_string = u"""Comment ça va ? Très bien ?"""
    
    print type(test_string),repr(test_string)
    utf8 = 'utf8:' + test_string.encode('UTF-8')
    print type(utf8), repr(utf8)
    
    c = pyodbc.connect('DSN=SA_SQL_SERVER_TEST;UID=XXX;PWD=XXX')
    
    cur = c.cursor()
    # This does not work as test_string is not UTF-encoded
    try: 
        cur.execute('INSERT unicode_test(t) VALUES(?)', test_string)
        c.commit()
    except pyodbc.Error,e:
        print e
    
    
    # This one does:
    try:
        cur.execute('INSERT unicode_test(t) VALUES(?)', utf8)
        c.commit()
    except pyodbc.Error,e:
        print e    
    
    
    

    Here is the output from the test table (I had manually put in a bunch of test data via Management Studio)

    In [41]: for i in cur.execute('SELECT t FROM unicode_test'):
       ....:     print i
       ....:
       ....:
    ('this is not a banana', )
    ('\xc3\x85kergatan 24', )
    ('\xc3\x85kergatan 24', )
    ('\xe6\xb0\xb4 this is code-point 63CF', )
    ('Mich\xc3\xa9l', )
    ('Comment a va ? Trs bien ?', )
    ('utf8:Comment \xc3\xa7a va ? Tr\xc3\xa8s bien ?', )
    

    I was able to put in some in unicode code points directly into the table from Management Studio by the 'Edit Top 200 rows' dialog and entering the hex digits for the unicode code point and then pressing Alt-X

提交回复
热议问题