2017-02-28 76 views
0

源代码如下:pyodbc 4.0.13:不支持Unicode的SQL语句在中国

# coding=utf8 

import pyodbc 

print pyodbc.version 

sql = u"""SELECT 
    N'我的' AS [Name] 
""" 


def test_success(): 
    conn = pyodbc.connect(u'Driver={SQL Server};Server=localhost;Database=mydb;Trusted_Connection=yes;', 
     unicode_results=True) 
    cursor = conn.cursor() 

    cursor.execute(sql) 
    print "test_success::" 
    print "." * 80 
    # data = cursor.fetchall() 
    print "." * 80 


test_success() 

有错误报道:

PS C:\Users\vincent\Documents\GitHub> python C:\Users\vincent\Downloads\pyodbctest.py 
4.0.13 
test_success:: 
................................................................................ 
................................................................................ 
Exception UnicodeEncodeError: UnicodeEncodeError('ascii', u"SELECT\n N'\u6211\u7684' AS [Name]\n", 13, 15, 'ordinal not in range(128)') in <module 'threading' from 'C:\Python27\lib\threading.pyc'> ignored 

有谁知道它是一个错误或我只是写错误的代码?

,这是非常奇怪的是,打印语句运行成功,但错误是从cursor.execute(sql)

+0

这个问题还正在对[GitHub上(讨论https://github.com/mkleehammer/pyodbc/issues/ 206)。 –

+0

这是我的问题,只是希望更多的人看到它 –

回答

0

我搜索了很多由谷歌,但没有回答我。所以我将pyodbc版本降级到版本4之前的最后一个版本3.1.1,它对我来说工作正常,从现在起我还没有发现类似的问题。

我的回答这个问题现在是:使用最新稳定版本pyodbc:3.1.1

+0

代码更改[已提交](https://github.com/mkleehammer/pyodbc/commit/13cf3503289dadbcad20083211128849e6d20ac7)在pyodbc版本4.0.14中解决此问题。 –