Search code examples
djangopyodbcfreetdsunixodbc

Issue with saving cyrillic symbols to MSSQL via unixODBC and FreeTDS


My environment is quite qimilar to this

All non-unicode data from database are displayed correctly. I am able to save some data to database (both ascii and cyrillic). Standard ascii characters are then displayed correctly, but Cyrillic data displayed like this

Р В Р’В Р вР

How can I find the point where wrong encode/decode operetion takes place?


Solution

  • I finally solved a problem. The correct database configuration in settings.py file is:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'db',
            'USER': 'user',
            'PASSWORD': 'pass',
            'HOST': '192.168.11.11',
            'PORT': '3306',
        },
        'mssql': {
            'ATOMIC_REQUESTS': True,
            'NAME': 'DB',
            'ENGINE': 'django_pyodbc',
            'USER': 'admin',
            'PASSWORD': 'pass',
            'OPTIONS': {
                'dsn': 'DBDSN',
                'host_is_server': True,
                'collation': 'Cyrillic_General_CI_AS',
                'driver_supports_utf8': True,
                'unicode_results':  True,
                'autocommit': True,
            },
        }
    }
    

    /etc/freetds/freetds.conf

    [global]
    tds version = 7.3
    text size = 64512
    
    [SRV]
    host = mssql.local
    port = 1433
    

    /etc/odbc.ini

    [ODBC Data Sources]
    DBDSN = Microsoft SQL Server
    
    [DBDSN]
    Driver = FreeTDS
    Servername = SRV
    Trace = No
    Database = DB
    

    /etc/odbcinst.ini

    [FreeTDS]
    Description = FreeTDS
    Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
    Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
    UsageCount = 1
    

    The important point is that odbc.ini and odbsinst.ini files should NOT contain spaces or tabs in the begining of the lines. These settings allows cyrillic symbols to be normally inserted into database.