UnicodeDecodeError when using pymssql with freetds
I want to get a field in SQL Server 2008 from python 2.6. Here is my freeTDS.conf file:
[ARGSERVER03]
host = 192.168.1.3
port = 1433
tds version = 7.0
Here is the code:
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
But it gives:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd3 in position 0: invalid continuation byte
The Unicode field is in Persian. Trace back for linecar = cur.fetchone_asdict()
[change]
I checked the database collation in the database properties from sql server management studio and this:
Arabic_CI_AS
But when I use this encoded it gives:
LookupError: unknown encoding: Arabic_CI_AS
source to share
Are you sure SQL Server is using UTF-8 (denoted by yours charset='UTF-8'
)? Generally, most of the SQL Server instances I have used Microsoft encoding (not UTF-8) such as cp1252 (in the US).
Several things that can help you find the correct encoding:
SELECT DATABASEPROPERTYEX('dbname', 'Collation') SQLCollation
source to share