Skip to content

Instantly share code, notes, and snippets.

@zxc111
Created December 18, 2015 07:08
Show Gist options
  • Save zxc111/89d70e9dd72ebf30d340 to your computer and use it in GitHub Desktop.
Save zxc111/89d70e9dd72ebf30d340 to your computer and use it in GitHub Desktop.
# coding: utf8
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, Integer, String, Sequence, SmallInteger
Base = declarative_base()
engine = create_engine(
'mysql+mysqldb://[email protected]/user_api?charset=utf8',
echo=False,
)
Session = sessionmaker(bind=engine)
session = Session()
class UserAvatar(Base):
__tablename__ = "user_avatar1"
id = Column(Integer, primary_key=True, autoincrement=True)
uid = Column(Integer,)
Base.metadata.create_all(bind=engine)
aaa = UserAvatar(uid=u"测试")
session.add(aaa)
session.commit()
Traceback (most recent call last):
File "test.py", line 30, in <module>
session.commit()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 801, in commit
self.transaction.commit()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 392, in commit
self._prepare_impl()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 372, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2015, in flush
self._flush(objects)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2133, in _flush
transaction.rollback(_capture_exception=True)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2097, in _flush
flush_context.execute()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 373, in execute
rec.execute(self)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 532, in execute
uow
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 174, in save_obj
mapper, table, insert)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 785, in _emit_insert_statements
execute(statement, params)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 914, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1146, in _execute_context
context)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1344, in _handle_dbapi_exception
util.reraise(*exc_info)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
context)
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 207, in execute
if not self._defer_warnings: self._warning_check()
File "/usr/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 117, in _warning_check
warn(w[-1], self.Warning, 3)
File "/usr/local/lib/python2.7/warnings.py", line 33, in _show_warning
file.write(formatwarning(message, category, filename, lineno, line))
File "/usr/local/lib/python2.7/warnings.py", line 42, in formatwarning
s = "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 26-27: ordinal not in range(128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment