python SQLalchemy 库的 Sqlite 数据库非 ascii 字符错误
当我将 sqlite3 数据库与 sqlalchemy 库一起使用时,我收到此错误
sqlalchemy.exc.ProgrammingError: (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.
u'INSERT INTO model_pair (user, password) VALUES (?, ?)' ('\xebE\xc2\xe4.\[email protected]', '123456')
,这是一些测试数据:
呆呆 [email protected] 11111
�言 [email protected] 11111
wwwj55572@gg?€? 11111
我已将数据库编码配置为 utf-8 或 gbk,但都没有成功 插入时,我尝试 str.decode('gbk'),它会卡在像 € 这样的字符上并收到如上所述的错误。
有人告诉我如何解决这个错误吗?
When i use sqlite3 database with sqlalchemy library, i got this error
sqlalchemy.exc.ProgrammingError: (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.
u'INSERT INTO model_pair (user, password) VALUES (?, ?)' ('\xebE\xc2\xe4.\[email protected]', '123456')
and here is some test data:
呆呆 [email protected] 11111
�言 [email protected] 11111
wwwj55572@gg?€? 11111
I have configured database encoding as utf-8 or gbk but neither success
when insert, i try str.decode('gbk'), it will stuck on char like € and get error like above.
anyone tell me how get around this error ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更改
'\xebE\xc2\xe4.\[电子邮件受保护]< /a>'
至u'\xebE\xc2\xe4.\
[电子邮件受保护]'
另外,你可以尝试这样做
'\xebE\xc2\xe4.\[电子邮件受保护] '.decode("utf-8")
,但它给出了一个错误“无效的连续字节”,也许你的字符串不是有效的 utf-8?顺便说一句,请注意您正在运行 python 2.x 或 3.x,这是有区别的。
try to change
'\xebE\xc2\xe4.\[email protected]'
tou'\xebE\xc2\xe4.\[email protected]'
also, you could try to do
'\xebE\xc2\xe4.\[email protected]'.decode("utf-8")
, but it gives an error "invalid continuation byte", perhaps your string is not valid utf-8 after all?btw, do mention is you are running python 2.x or 3.x, there is a difference.