首页   注册   登录
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python 学习手册
Python Cookbook
Python 基础教程
Python Sites
PyPI - Python Package Index
http://www.simple-is-better.com/
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 · 72 天前 · 1099 次点击
    这是一个创建于 72 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码 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   72 天前
    读中文到是没有问题
    mon3
        2
    mon3   72 天前
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    换 python3 吧
    Ritter
        3
    Ritter   72 天前
    s
    Ritter
        4
    Ritter   72 天前
    ca 按错了 是不是数据库编码问题
    chizuo
        5
    chizuo   72 天前
    把 mysql 编码格式改为 utf8emb4
    4everLoveU
        6
    4everLoveU   72 天前
    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   71 天前
    @mon3 已经是 python36
    holinhot
        8
    holinhot   71 天前
    @4everLoveU 数据库编码是 utf-8,写入的中文字符串打印在屏幕是正常的,没有乱码。从数据库读出来的也正常。就是写入不行
    msy45
        9
    msy45   69 天前
    mysql 默认的 utf-8 是 utf8mb3,改成 utf8mb4 试试吧
    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2724 人在线   最高记录 5168   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:04 · PVG 18:04 · LAX 03:04 · JFK 06:04
    ♥ Do have faith in what you're doing.