sqlite3 and UTF-8

Peter Otten __peter__ at web.de
Tue Dec 7 11:52:00 EST 2010


Ale Ghelfi wrote:

> i try this :
> 
>> actual_encoding = ... # whatever
>> def decode(s):
>>      return s.decode(actual_encoding)
>>
>> db = sqlite3.connect(...)
>> db.text_factory = decode
> 
> but now the error is :
> 
>  >>> rowset = cur.fetchall()
> Traceback (most recent call last):
>    File "<pyshell#40>", line 1, in <module>
>      rowset = cur.fetchall()
>    File "<pyshell#34>", line 2, in decode
>      return s.decode(enc)
>    File "/usr/lib/python2.6/encodings/utf_8.py", line 16, in decode
>      return codecs.utf_8_decode(input, errors, True)
> UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in position 33:
> invalid start byte

So you specified

actual_encoding = "UTF-8"

? That's pointless because UTF-8 is the default, and you've already seen 
that failing. You can set

db.text_factory = str

but you'll probably run into problems with that later, e. g. when you try to 
display the retrieved data.

So again, what data do you expect to find in the column(s) you can't decode 
properly?

Peter



More information about the Python-list mailing list