methew / pyodbc

Automatically exported from code.google.com/p/pyodbc
MIT No Attribution
0 stars 0 forks source link

unicode_results is not honored when connecting to SQL Server Express Edition #284

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use django-pyodbc to connect to SQL Server Express Edition running on AWS 
RDS.
2. Data going in seems to be utf8 encoded properly.
3. Data coming out seems to be decoded as something else and it bubbles back to 
Django as unicode objects full of garbage where non-ascii characters are 
supposed to be.

What is the expected output? What do you see instead?

I see strings like 'Jõuülekande osad' instead of 'Jõuülekande osad'.

django-pyodbc connects to the database as:

            if self.unicode_results:
                self.connection = Database.connect(connstr, \
                        autocommit=autocommit, \
                        unicode_results='True')
            else:
                self.connection = Database.connect(connstr, \
                        autocommit=autocommit)

In format_results() it has the following code:

        for row in rows:
            if isinstance(row, str):
                fr.append(row.decode('utf-8'))
            else:
                fr.append(row)

However, cursor.execute() returns nvarchar() columns as unicode objects even 
though unicode_results = False. By my logic it should return them as str 
objects so that django-pyodbc can do the proper decoding itself. Why is pyodbc 
not honoring the unicode_results flag?

What version of the product are you using? On what operating system?

pyodbc version 3.0.6, django-pyodbc is from 
https://github.com/elver/django-pyodbc

Original issue reported on code.google.com by elver.loho@gmail.com on 6 Sep 2012 at 2:57

GoogleCodeExporter commented 9 years ago
Adding the following to connection params seems to have made the driver return 
properly-decoded unicode objects, at the very least, so it kinda works as a 
workaround for now.

'extra_params' : "TDS_VERSION=8.0;ClientCharset=UTF-8",

Original comment by elver.loho@gmail.com on 6 Sep 2012 at 3:31

GoogleCodeExporter commented 9 years ago
Also, when possible, I recommend using Microsoft's driver for Linux instead of 
FreeTDS.  It has significantly fewer problems.

Original comment by mkleehammer on 26 Sep 2012 at 8:45