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.

  • 相关阅读:
    linux常用脚本
    shell学习笔记
    Linux常用命令List
    Centos7部署Zabbix
    centos7安装nagios步骤
    nagios报错HTTP WARNING: HTTP/1.1 403 Forbidden解决方法
    U盘安装CentOS7
    Thread线程控制之sleep、join、setDaemon方法的用处
    EfficientDet框架详解 | 目前最高最快最小模型,可扩缩且高效的目标检测(附源码下载)
    ubuntu18.04 安装多版本cuda ,原来版本为9.0,在新增8.0
  • 原文地址:https://www.cnblogs.com/lexus/p/1845326.html
Copyright © 2011-2022 走看看