Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python insert UTF8 string into SQLite

I know there are similar questions, but the answers are distinct and kind of confusing.

I have this string:

titulo = "Así Habló Zaratustra (Cómic)"

When I try to insert it to the SQLite database I get the 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.

I've tried a couple of things without success. Please help.

like image 998
Alejandro Veintimilla Avatar asked Feb 12 '23 22:02

Alejandro Veintimilla


1 Answers

Do what it tells you to do and use unicode values instead:

titulo_unicode = titulo.decode('utf8')

The sqlite3 library will take care of encoding this correctly when inserting, decoding again when selecting.

like image 190
Martijn Pieters Avatar answered Feb 14 '23 13:02

Martijn Pieters