Python insert UTF8 string into SQLite

浪尽此生 提交于 2020-01-02 08:33:53

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/26079693/python-insert-utf8-string-into-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!