zoukankan      html  css  js  c++  java
  • ProgrammingError: You must not use 8bit bytestrings unless you use a text_factory that can interpret 8bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings

    pysqlite插入UTF-8的bytestring

    Python 查看评论

    报错

    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

    修正方法,按照提示
    加入  
    conn.text_factory = str

    或者将应用切换为Unicode 字符

    conn.text_factory = lambda x: unicode(x, ‘utf-8′, ‘ignore’)

    By default text_factory is set to unicode(), which will use the current default encoding (ascii on my machine)

    或者插入字符串前,强制转换为unicode

    请查看 PySQLite的文档

    http://pysqlite.googlecode.com/svn/doc/sqlite3.html#sqlite3.Connection.text_factory

    http://docs.python.org/library/sqlite3.html#sqlite3.Connection.text_factory

    请看类似的修正

    http://bazaar.launchpad.net/~francesco-marella/entertainer/fix-bug-315247/revision/407?remember=407

    http://issues.roundup-tracker.org/issue2277204

    这个问题只在  
    Python 2.6/sqlite3 或者 Python 3.x中出现

    我在Debian Squeeze中默认使用 Python 2.5 就没事
    在 Ubuntu 10.04 中使用Python 2.6 就有这个问题

    在Django中是这么修复的

    if Database.version_info >= (2,4,1):
    # Starting in 2.4.1, the str type is not accepted anymore, therefore,
    # we convert all str objects to Unicode
    # As registering a adapter for a primitive type causes a small
    # slow-down, this adapter is only registered for sqlite3 versions
    # needing it.
    Database.register_adapter(str, lambda s:s.decode(‘utf-8′))

    In Py3, anything you do “this” or use str(), you are using unicode strings.

    The problem appears to be that you are passing bytestrings to sqlite; things
    created with b”this” or bytes(), or read from a file as bytes and not
    decoded before passing it to the database.

    To really help further, you should provide the line that threw that warning
    and show where any variables in it come from. As for that error message, I
    believe it means text_factory = bytes.

  • 相关阅读:
    Android之剑法初略:dalvik vm和jvm比较
    人民币阿拉伯数字转换为汉字大写 code
    数据库定时备份方案及实践
    [postfix]添加黑名单
    [php][随机数]曲线式的随机
    记公司服务器维护经历
    批量修改符号链接实现思路
    复杂数组的签名生成方法
    [已解决]ubuntu下chrome和firefox输入框内无法快捷键全选
    [已解决]centos6.4 php连接mysql和memcache提示权限不允许
  • 原文地址:https://www.cnblogs.com/lexus/p/1845326.html
Copyright © 2011-2022 走看看