Search code examples
pythonunicodefdb

Python 3 + FDB: UnicodeDecodeError


I want to connect to Firebird 2.1 DB that uses cp1251, execute a statement and get the result. Here's what I do:

import fdb

def get_zone(reg, sps):
    con = fdb.connect(
        dsn='172.16.16.77:database',
        user='SYSDBA', password='1234',
        sql_dialect=3, charset='WIN1251'
    )
    cur = con.cursor()
    select = ("SELECT ZONE "
          "FROM ZONES "
          "WHERE ZONE_NAME LIKE "
          + reg[1:-3] + "% "
          "AND ZONE < 600000 "
          "AND ZONE NAME CONTAINING 'СПС'")
    if not sps:
        select = select[:-16] + 'NOT' + select[-17:]
    cur.execute(select)
    return cur[0]

cur.execute(select) interrupts with an error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 0: unexpected end of data
I want to know two things:

  1. How do I get rid of the error
  2. Is the fact that Python 3 works primarily with Unicode dangerous in context of accessing a WIN1251 encoded database? If so, guide me a bit about what should I do/avoid/etc.

To @VivekSable question: right before the error the select variable contains following string:
SELECT ZONE FROM ZONES WHERE ZONE_NAME LIKE 'Краснодарский кр%' AND ZONE < 600000 AND ZONE NAME CONTAINING 'СПС'


Solution

  • Any query string sent to WIN1251 database should be initially processed with .encode('cp1251') method.