我有一个带有 Unicode 列的 SQLAlchemy 模型。我有时会向它插入 unicode 值 (u'Value'),但有时也会插入 ASCII 字符串。最好的方法是什么?当我插入带有特殊字符的 ASCII 字符串时,我收到此警告:
SAWarning: Unicode type received non-unicode bind param value ...
如何避免这种情况?插入不同类型字符串的正确方法是什么?
最佳答案
有几种选择:
- 使用
warnings.simplefilter('ignore', sqlalchemy.exc.SAWarning)
禁用所有 SQLAlchemy 警告。 - 仅通过模块和 lineno 或消息使用更具体的过滤器规范禁用此警告,例如
warnings.filterwarnings('ignore', '^Unicode 类型收到非 unicode 绑定(bind)参数值', sqlalchemy.exc.SAWarning)
。 - 使用
String(convert_unicode=True)
代替Unicode
类型。 - 重新考虑问题并更改代码以对 ASCII 字符串使用 unicode。
关于python - 将字符串插入 SQLAlchemy Unicode 列的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5735242/