You must not use 8-bit bytestrings

Added by Christoph Kappel 10 months ago

One of my many python scripts suddenly presented me with following error:

sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

After a hell of a search with many hints and possible solutions here is a one liner that fixes this issue for sure: Just set the text_factory property right after opening the connection:

conn = sqlite3.connect("../database.db" 
conn.text_factory = lambda x: unicode(x, "utf-8", "ignore")

Also available in: Atom RSS