mamcx
mamcx

Reputation: 16194

UnicodeError with SqlAlchemy + Firebird + FDB

I'm triying to display results from a firebird 3.x database, but get:

File "/...../Envs/pos/lib/python3.6/site-packages/fdb/fbcore.py", line 479, in b2u return st.decode(charset) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd1 in position 9: invalid continuation byte

Despite I set utf-8 everywhere:

# -- coding: UTF-8 -- 

import os

os.environ["PYTHONIOENCODING"] = "utf8"

from sqlalchemy import *

SERVIDOR = "localhost"
BASEDATOS_1 = "db.fdb"

PARAMS = dict(
    user="SYSDBA",
    pwd="masterkey",
    host="localhost",
    port=3050,
    path=BASEDATOS_1,
    charset='utf-8'
)

firebird = create_engine("firebird+fdb://%(user)s:%(pwd)s@%(host)s:%(port)d/%(path)s?charset=%(charset)s" % PARAMS, encoding=PARAMS['charset'])

def select(eng, sql):
    with eng.connect() as con:
        return eng.execute(sql)

for row in select(firebird, "SELECT * from clientes"):
    print(row)

Upvotes: 5

Views: 1139

Answers (2)

roeland
roeland

Reputation: 6336

I had the same problem.
In my situation the database was not in UTF-8. After setting the correct charset in the connection string it worked: ?charset=ISO8859_1

Upvotes: 5

Joao Maciel
Joao Maciel

Reputation: 11

I would try to use the module unidecode.

Your script is crashing when it tries to convert, so this module can help you. As they says in the module documentation:

The module exports a single function that takes an Unicode object (Python 2.x) or string (Python 3.x) and returns a string (that can be encoded to ASCII bytes in Python 3.x)

First you download it using pip and then try this:

import unidecode

...
if type(line) is unicode:
   line = unidecode.unidecode(line)

I hope it solves your problem.

Upvotes: 1

Related Questions