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中直接插入中文没有问题:
這種場景要用
unicode
類型:你的python預設是gbk