python - Flask-SQLAlchemy插入中文到MySQL时出现问题?
PHPz
PHPz 2017-04-17 16:44:39
0
2
1622
from app import db,User
user=User(username='我')
db.session.add(user)
print(user.username)

此时输出正常

当我

db.session.commit()

之后出现了错误

c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\engine\default.py:450: Warning: Incorrect string value: '\xD6\xD0\xB9\xFA\xB1\xEA...' for column 'VARIABLE_VALUE' at row 475
  cursor.execute(statement, parameters)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\scoping.py", line 150, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\session.py", line 813, in commit
    self.transaction.commit()
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\session.py", line 392, in commit
    self._prepare_impl()
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\session.py", line 372, in _prepare_impl
    self.session.flush()
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2027, in flush
    self._flush(objects)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2145, in _flush
    transaction.rollback(_capture_exception=True)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2109, in _flush
    flush_context.execute()
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 373, in execute
    rec.execute(self)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 532, in execute
    uow
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\persistence.py", line 174, in save_obj
    mapper, table, insert)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\orm\persistence.py", line 785, in _emit_insert_statements
    execute(statement, params)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\engine\base.py", line 914, in execute
    return meth(self, multiparams, params)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 323, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1010, in _execute_clauseelement
    compiled_sql, distilled_params
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1146, in _execute_context
    context)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1341, in _handle_dbapi_exception
    exc_info
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\util\compat.py", line 200, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1139, in _execute_context
    context)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\sqlalchemy\engine\default.py", line 450, in do_execute
    cursor.execute(statement, parameters)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\MySQLdb\cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "c:\Users\Sanch\Desktop\Reborn\venv\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorvalue
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1366, "Incorrect string value: '\\xCE\\xD4\\xB2\\xDB' for column 'username' at row 1") [SQL: u'INSERT INTO users (email, username, password, role_id) VALUES (%s, %s, %s, %s)'] [parameters: (None, '\xce\xd4\xb2\xdb', None, 2)]

MySQL中相应列的编码格式是UTF-8,在MySQL中直接插入中文没有问题:

PHPz
PHPz

学习是最好的投资!

모든 응답(2)
洪涛

이 시나리오에서는 유니코드 유형을 사용하세요.

user=User(사용자 이름=u'我')
阿神

파이썬 기본값은 gbk입니다

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿