V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
holinhot
V2EX  ›  Python

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

  •  
  •   holinhot · 2020-04-24 06:20:20 +08:00 · 2764 次点击
    这是一个创建于 1682 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码 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)
    9 条回复    2020-04-27 09:34:18 +08:00
    holinhot
        1
    holinhot  
    OP
       2020-04-24 06:48:21 +08:00
    读中文到是没有问题
    mon3
        2
    mon3  
       2020-04-24 09:02:59 +08:00
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    换 python3 吧
    Ritter
        3
    Ritter  
       2020-04-24 09:46:17 +08:00
    s
    Ritter
        4
    Ritter  
       2020-04-24 09:47:19 +08:00
    ca 按错了 是不是数据库编码问题
    chizuo
        5
    chizuo  
       2020-04-24 10:18:11 +08:00
    把 mysql 编码格式改为 utf8emb4
    4everLoveU
        6
    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
        7
    holinhot  
    OP
       2020-04-24 20:43:57 +08:00
    @mon3 已经是 python36
    holinhot
        8
    holinhot  
    OP
       2020-04-24 20:47:33 +08:00
    @4everLoveU 数据库编码是 utf-8,写入的中文字符串打印在屏幕是正常的,没有乱码。从数据库读出来的也正常。就是写入不行
    msy45
        9
    msy45  
       2020-04-27 09:34:18 +08:00
    mysql 默认的 utf-8 是 utf8mb3,改成 utf8mb4 试试吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2664 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 09:42 · PVG 17:42 · LAX 01:42 · JFK 04:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.