将字符串插入 SQLAlchemy Unicode 列的正确方法
我有一个带有 Unicode 列的 SQLAlchemy 模型。我有时会向其中插入 unicode 值 (u'Value'),但有时也会插入 ASCII 字符串。解决这个问题的最佳方法是什么?当我插入带有特殊字符的 ASCII 字符串时,我收到此警告:
SAWarning: Unicode type received non-unicode bind param value ...
如何避免这种情况?插入不同类型字符串的正确方法是什么?
I have a SQLAlchemy model with a Unicode column. I sometimes insert unicode values to it (u'Value'), but also sometimes insert ASCII strings. What is the best way to go about this? When I insert ASCII strings with special characters I get this warning:
SAWarning: Unicode type received non-unicode bind param value ...
How do I avoid this? What is the proper way to insert my different types of strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几个选项:
warnings.simplefilter('ignore', sqlalchemy.exc.SAWarning)
禁用所有 SQLAlchemy 警告。warnings.filterwarnings('ignore', '^Unicode 类型收到非 unicode 绑定参数值', sqlalchemy.exc.SAWarning)
。String(convert_unicode=True)
而不是Unicode
类型。Thre are several options:
warnings.simplefilter('ignore', sqlalchemy.exc.SAWarning)
.warnings.filterwarnings('ignore', '^Unicode type received non-unicode bind param value', sqlalchemy.exc.SAWarning)
.String(convert_unicode=True)
instead ofUnicode
type.您应该这样定义表类和构造函数:
当然,您不应该忘记为
create_engine 函数
附加URI+"charset=utf8"
You should define your table class and constructor as such:
Of course, you should not forget to attach
URI+"charset=utf8"
forcreate_engine function