【问题标题】:Python insert UTF8 string into SQLitePython将UTF8字符串插入SQLite
【发布时间】:2014-09-27 22:15:12
【问题描述】:

我知道有类似的问题,但答案是不同的,有点令人困惑。

我有这个字符串:

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

当我尝试将其插入 SQLite 数据库时,我收到错误:

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.

我尝试了几件事,但都没有成功。请帮忙。

【问题讨论】:

    标签: python sqlite encoding utf-8


    【解决方案1】:

    按照它告诉你的去做,并改用unicode 值:

    titulo_unicode = titulo.decode('utf8')
    

    sqlite3 库会在插入时正确编码,在选择时再次解码。

    【讨论】:

      猜你喜欢
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多