2012-04-04 20 views
3

我想從python 2.6中獲取SQL Server 2008中的字段。這是我的freetds的.conf文件:在freetds中使用pymssql時的UnicodeDecodeError

[ARGSERVER03] 
    host = 192.168.1.3 
    port = 1433 
    tds version = 7.0 

這裏是代碼:

conn = pymssql.connect(host='192.168.1.3', user='****', password='****', database='TrafficMonitor', as_dict=True, charset='UTF-8') 
i = 0 
cur.execute('SELECT * FROM dbo.tblTrafficCounterData') 
while i < 10: 
    car = cur.fetchone_asdict() 
    if car is None: 
     break 
    c = car['Class'] 
    print c 
    i = i + 1 

但它給:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xd3 in position 0: invalid continuation byte 

Unicode的領域是波斯語。該追溯是線car = cur.fetchone_asdict()

[編輯]

我已經從SQL Server Management Studio數據庫性能檢查數據庫歸類,它是:

Arabic_CI_AS 

但是,當我使用在它給出的字符集:

LookupError: unknown encoding: Arabic_CI_AS 
+0

這個錯誤似乎是非常類似:HTTP:/ /stackoverflow.com/questions/9090915/how-to-read-large-file-with-unicode-in-python-3 – 2012-04-04 12:45:02

回答

4

您是否確信SQL Server正在使用UTF-8(指示您的charset='UTF-8')?通常,我所使用的大多數SQL Server實例都使用Microsoft編碼(而不是UTF-8),例如cp1252(在美國)。

幾件事情,可以幫助您找到正確的編碼:

SELECT DATABASEPROPERTYEX('dbname', 'Collation') SQLCollation

+0

查看更新以獲取更多信息 – 2012-04-05 06:38:52

+1

Arabic_CI_AS是排序規則的SQL Server名稱,它僅提供有關要使用的Python編碼的提示。嘗試使用'cp1256'作爲字符集('cp1256'用於Windows阿拉伯語,如http://docs.python.org/library/codecs.html#standard-encodings所示) – 2012-04-06 13:54:46

+0

謝謝你,'cp1256'的作品。 – 2012-04-07 03:56:45