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.

  • 相关阅读:
    java多线程练习题 类
    java练习题在一个文件里面输入内容在另一个文件里面可以查看
    java练习题输入流姓名学号信息
    java 异常处理2
    java 处理异常练习题
    java get银行练习题
    java 练习题 求梯形的面积和周长
    java get正确写类的练习题 猫
    GUID 全局唯一标识符
    oracle 建表 练习2
  • 原文地址:https://www.cnblogs.com/lexus/p/1845326.html
Copyright © 2011-2022 走看看