Python将UTF8字符串插入到SQLite中 [英] Python insert UTF8 string into SQLite

查看:169
本文介绍了Python将UTF8字符串插入到SQLite中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有类似的问题,但答案是有区别的,令人困惑的。



我有这个字符串:

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



  sqlite3.ProgrammingError:您不能使用除非你使用可以解释8位bytestrings的text_factory(如text_factory = str),否则8位的bytestrings。强烈建议您将应用程序切换为Unicode字符串。 

我没有成功尝试过几件事情。请帮助。

解决方案

做什么告诉你做和使用 unicode 值代替:

  titulo_unicode = titulo.decode('utf8')
/ pre>

当插入时, sqlite3 库将正确地处理编码,再次选择时解码。 p>

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.

解决方案

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.

这篇关于Python将UTF8字符串插入到SQLite中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆