问一下 flask sqlalchemy 往 mysql 写不了中文

2020-04-24 06:20:20 +08:00
 holinhot
代码 820 行为 db.session.commit()

错误代码
Traceback (most recent call last):
File "/home/app_sandbox/api/api-service/app.py", line 820, in new_order
db.session.commit()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1027, in commit
self.transaction.commit()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 494, in commit
self._prepare_impl()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 473, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2459, in flush
self._flush(objects)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2597, in _flush
transaction.rollback(_capture_exception=True)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2557, in _flush
flush_context.execute()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 1138, in _emit_insert_statements
statement, params
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 988, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
distilled_params,
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
e, statement, parameters, cursor, context
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1468, in _handle_dbapi_exception
util.reraise(*exc_info)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
cursor, statement, parameters, context
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib64/python3.6/site-packages/MySQLdb/cursors.py", line 202, in execute
args = tuple(map(db.literal, args))
File "/usr/local/lib64/python3.6/site-packages/MySQLdb/connections.py", line 271, in literal
s = self.escape(o, self.encoders)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 11-12: ordinal not in range(128)
'ascii' codec can't encode characters in position 11-12: ordinal not in range(128)
2776 次点击
所在节点    Python
9 条回复
holinhot
2020-04-24 06:48:21 +08:00
读中文到是没有问题
mon3
2020-04-24 09:02:59 +08:00
import sys
reload(sys)
sys.setdefaultencoding('utf8')
换 python3 吧
Ritter
2020-04-24 09:46:17 +08:00
s
Ritter
2020-04-24 09:47:19 +08:00
ca 按错了 是不是数据库编码问题
chizuo
2020-04-24 10:18:11 +08:00
把 mysql 编码格式改为 utf8emb4
4everLoveU
2020-04-24 10:52:28 +08:00
mysqldb 的用法,供参考,写入数据之前保证数据是 UTF-8 编码,以及数据库编码也是 UTF8

cursor.execute("SET NAMES utf8mb4;")
cursor.execute("SET CHARACTER SET utf8mb4;")
cursor.execute("SET character_set_connection=utf8mb4;")
holinhot
2020-04-24 20:43:57 +08:00
@mon3 已经是 python36
holinhot
2020-04-24 20:47:33 +08:00
@4everLoveU 数据库编码是 utf-8,写入的中文字符串打印在屏幕是正常的,没有乱码。从数据库读出来的也正常。就是写入不行
msy45
2020-04-27 09:34:18 +08:00
mysql 默认的 utf-8 是 utf8mb3,改成 utf8mb4 试试吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/665520

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX