sqlalchemy - (ProgrammingError) 可以解释 8 位字节串 [英] sqlalchemy - (ProgrammingError) can interpret 8-bit bytestrings

查看:45
本文介绍了sqlalchemy - (ProgrammingError) 可以解释 8 位字节串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 2.7.6 做 SqlAlchemy

I'm using do SqlAlchemy with python 2.7.6

我遇到了:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) 不得使用 8 位字节除非您使用可以解释 8 位字节串的 text_factory(例如text_factory = str).强烈建议您改为只切换您的Unicode 字符串的应用.u'INSERT INTO algoritimo(nomeAlgoritimo",类se, "estruturaDados", "complexidadePiorCaso", "complexidadeMedioCaso", "complexidadeMelhorCaso", "complexidadeEspacos", "pseudoAlgoritimo") VALUES (?, ?, ?, ?,?, ?, ?, ?)' ('Ordena\xe7\xe3o Sele\xe7\xe3o', 'Algoritmo de oderna\xe7\xe3o', '数组,Listas ligadas', 'O(n^2)', 'O(n^2)', 'O(n^2)', 'O(n) total, O(1) 辅助', '')

sqlalchemy.exc.ProgrammingError: (ProgrammingError) You must not use 8-bit bytes trings 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. u'INSERT INTO algoritimo ("nomeAlgoritimo", clas se, "estruturaDados", "complexidadePiorCaso", "complexidadeMedioCaso", "complexi dadeMelhorCaso", "complexidadeEspacos", "pseudoAlgoritimo") VALUES (?, ?, ?, ?, ?, ?, ?, ?)' ('Ordena\xe7\xe3o Sele\xe7\xe3o', 'Algoritmo de oderna\xe7\xe3o', ' Array, Listas ligadas', 'O(n^2)', 'O(n^2)', 'O(n^2)', 'O(n) total, O(1) auxiliar ', '')

我使用:

Base = declarative_base()engine = create_engine('sqlite:///classificacao_pesquisa.db')

Base = declarative_base() engine = create_engine('sqlite:///classificacao_pesquisa.db')

class Algoritimo(Base):
    __tablename__ = 'algoritimo'
    id = Column(Integer, primary_key=True)
    nomeAlgoritimo = Column(String(50))
    classe = Column(String(250))
    estruturaDados = Column(String(50))
    complexidadePiorCaso = Column(String(50))
    complexidadeMedioCaso = Column(String(50))
    complexidadeMelhorCaso = Column(String(50))
    complexidadeEspacos = Column(String(50))
    pseudoAlgoritimo = Column(String(4000))

你有什么建议吗?

推荐答案

我遇到了同样的问题,这里是解决方案;

I've experienced the same issue and here is the solution;

将编码字符串强制转换为 Unicode

如上述文档中所述,db.Unicode 类型是不够的,您仍然必须以 u'some string' 的形式发送字符串;

As stated in the above documentation db.Unicode type would not be enough, you still have to send the string in the form of u'some string';

关于 Unicode 类型的一个常见混淆来源是它是旨在仅处理 Python 端的 Python unicode 对象,表示作为绑定参数传递给它的值必须采用以下形式u'some string' 如果使用 Python 2 而不是 3.编码/解码它执行的功能仅适用于使用中的 DBAPI 的要求,并且主要是一个私有的实现细节.

A common source of confusion regarding the Unicode type is that it is intended to deal only with Python unicode objects on the Python side, meaning values passed to it as bind parameters must be of the form u'some string' if using Python 2 and not 3. The encoding/decoding functions it performs are only to suit what the DBAPI in use requires, and are primarily a private implementation detail.

因此您可以使用 TypeDecorator 创建自己的类型;

So you can create your own Type using the TypeDecorator;

from sqlalchemy.types import TypeDecorator, Unicode

class CoerceUTF8(TypeDecorator):
    """Safely coerce Python bytestrings to Unicode
    before passing off to the database."""

    impl = Unicode

    def process_bind_param(self, value, dialect):
        if isinstance(value, str):
            value = value.decode('utf-8')
        return value

用法:

some_value = db.Column(CoerceUTF8)) 

这篇关于sqlalchemy - (ProgrammingError) 可以解释 8 位字节串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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