michiya / django-pyodbc-azure

Django backend for Microsoft SQL Server and Azure SQL Database using pyodbc
https://pypi.python.org/pypi/django-pyodbc-azure
BSD 3-Clause "New" or "Revised" License
321 stars 140 forks source link

Handle decode charset error #86

Open JosephDev opened 7 years ago

JosephDev commented 7 years ago

base.py line 561 - 571

def format_row(self, row):
    """
    Decode data coming from the database if needed and convert rows to tuples
    (pyodbc Rows are not sliceable).
    """
    if self.driver_charset:
        for i in range(len(row)):
            f = row[i]
            # FreeTDS (and other ODBC drivers?) doesn't support Unicode
            # yet, so we need to decode utf-8 data coming from the DB
            if isinstance(f, binary_type):
                row[i] = f.decode(self.driver_charset)
    return row

In case fail to decode, it raise error. What if we decode with error ignore option like f.decode(self.driver_charset, errors ='ignore')

If you need, I am going to make PR.

sean29 commented 6 years ago

getting this error: is it related?

UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 22-23: unexpected end of data

singhravi1 commented 5 years ago

@sean29 Were you able to solve this?

Edit: I was getting this because of inputting invalid value in the geography field in MSSQL database