sqlite3 and UTF-8

Peter Otten __peter__ at web.de
Tue Dec 7 10:55:29 EST 2010


Ale Ghelfi wrote:

> I try to connect a database sqlite by sqlite3, but return an error.
> 
>  >>> rowset = cur.fetchall()
> Traceback (most recent call last):
>    File "<pyshell#20>", line 1, in <module>
>      rowset = cur.fetchall()
> OperationalError: Could not decode to UTF-8 column 'DATO' with text
> 'Document n°10'
> 
> What's happend? thank you

How did you enter the data into the database? If it was with a script under 
your control modify it to feed unicode instead of str to the database.

Otherwise, if you know the encoding used in the database maybe setting  
Connection.text_factory

actual_encoding = ... # whatever
def decode(s): 
    return s.decode(actual_encoding)

db = sqlite3.connect(...)
db.text_factory = decode


helps (untested). See also

http://docs.python.org/library/sqlite3.html#sqlite3.Connection.text_factory

Peter



More information about the Python-list mailing list