4

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. The trace back is for line car = cur.fetchone_asdict()

[Edit]

I have checked for database collation in database properties from sql server management studio and it is:

Arabic_CI_AS

But when I use that in charset it gives:

LookupError: unknown encoding: Arabic_CI_AS
1

1 Answer 1

4

Are you extremely sure that SQL Server is using UTF-8 (indicated by your charset='UTF-8')? Typically most SQL Server instances I have run into use a Microsoft encoding (not UTF-8), such as cp1252 (in the U.S.).

A few things that may help you discover the correct encoding:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Arabic_CI_AS is the SQL Server name for the collation, it only provides a hint as to the Python encoding to use. Try using cp1256 as the charset (cp1256 is for Windows Arabic, as shown at docs.python.org/library/codecs.html#standard-encodings)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.