【问题标题】:Unicode with Pyramid, SQLAlchemyUnicode 与 Pyramid、SQLAlchemy
【发布时间】:2011-11-01 04:59:49
【问题描述】:

我正在使用 Pyramid 和 SQLAlchemy,但以下简化代码:

u = u"\u201C"

m = M()
m.comment = u
m.user_id = 1
session.add( m )
session.commit()

给我一​​个

UnicodeEncodeError: 'latin-1' codec can't encode character u'\u201c' in position 0: ordinal not in range(256)

我需要什么来解决这个问题?

编辑:

comment 是“MEDIUMTEXT”列,MySQL 类型。

【问题讨论】:

  • 你能把类M的定义贴出来吗?

标签: python sqlalchemy pyramid


【解决方案1】:

检查您的数据库表是否使用 unicode 以外的编码进行编码。

另外,如果您希望您的 cmets 可以从 phpMyAdmin(或您用于连接数据库的任何设备)读取,您应该附加:

?charset=utf8&use_unicode=0

到应用程序配置文件中的连接字符串

所以你会有 mysql://user:pass@localhost/dbname?charset=utf8&use_unicode=0

【讨论】:

    【解决方案2】:

    您的comment 列很可能应该是Unicode 类型而不是String

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 2015-06-19
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      相关资源
      最近更新 更多