2

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 'СПС'

mekkanizer
  • 722
  • 2
  • 9
  • 28
  • Can you log `select` value before `cur.execute(select)` statement and shear with us? – Vivek Sable Feb 11 '15 at 08:41
  • I do not know this will work or not, but can you try like this `reg[1:-3].decode("utf-8")` – Vivek Sable Feb 11 '15 at 09:36
  • @VivekSable the addition of `.decode("utf-8")` raises another issue: `AttributeError: 'str' object has no attribute 'decode'` – mekkanizer Feb 11 '15 at 09:43
  • @VivekSable I don't know if this is anyhow related, but the `reg` variable is always filled with data previously read from `Windows-1251` encoded .csv; however, I invoke the `open()` method with `encoding='cp1251'` parameter, so... I dunno. I'm puzzled. – mekkanizer Feb 11 '15 at 09:50

1 Answers1

0

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

mekkanizer
  • 722
  • 2
  • 9
  • 28