我在尝试从MySQL数据库中自动执行表时遇到错误。 错误是:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)
当我制作一个:
时会发生此错误Base.prepare(engine, reflect=True)
我的数据库位于utf8_bin。
这是我与数据库的引擎连接:
engine = create_engine("mysql://User:passwd@127.0.0.1:3308/db?charset=utf8", encoding="utf-8", echo=True)
这是错误的完整堆栈跟踪:
2016-05-12 15:57:39,497 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2016-05-12 15:57:39,497 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,511 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2016-05-12 15:57:39,511 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,537 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8' and `Collation` = 'utf8_bin'
2016-05-12 15:57:39,538 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,552 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2016-05-12 15:57:39,552 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,566 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2016-05-12 15:57:39,566 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,580 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin AS anon_1
2016-05-12 15:57:39,580 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,620 INFO sqlalchemy.engine.base.Engine SHOW FULL TABLES FROM `GesCo`
2016-05-12 15:57:39,620 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,652 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `Client`
2016-05-12 15:57:39,652 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,670 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `ClientContact`
2016-05-12 15:57:39,670 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,686 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `Dossier`
2016-05-12 15:57:39,686 INFO sqlalchemy.engine.base.Engine ()
2016-05-12 15:57:39,705 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `RDV`
2016-05-12 15:57:39,705 INFO sqlalchemy.engine.base.Engine ()
Traceback (most recent call last):
File "./api.py", line 29, in <module>
Base.prepare(engine, reflect=True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/automap.py", line 788, in prepare
map_config.map()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/base.py", line 592, in map
return super(_DeferredMapperConfig, self).map()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/base.py", line 529, in map
**self.mapper_args
File "<string>", line 2, in mapper
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 627, in __init__
self._configure_properties()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1318, in _configure_properties
setparent=True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1607, in _configure_property
prop.instrument_class(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 182, in instrument_class
doc=self.doc
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 1489, in register_descriptor
manager.instrument_attribute(key, descriptor)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/instrumentation.py", line 215, in instrument_attribute
self.install_descriptor(key, inst)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/instrumentation.py", line 269, in install_descriptor
setattr(self.class_, key, inst)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)`
提前感谢您提供的任何帮助
编辑:
更改了mysql中的所有charset变量和collations变量,它仍会产生相同的错误:
mysql> show variables like 'collation%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
mysql> show variables like 'character\_set\_%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+--------+
答案 0 :(得分:1)
您是否尝试将character_set_client
等MySQL变量设置为utf-8
?您可能希望在MySQL中运行以下查询并将所有值设置为utf-8
:
mysql> show variables like 'character\_set\_%';
您还可以尝试将utf8_general_ci
之类的某些utf-8排序规则设置为输出中的变量:
mysql> show variables like 'collation%';
答案 1 :(得分:0)
看起来您的列名称中包含é
,而SQLAlchemy正在尝试将其用作模型类的属性名称,但Python 2仅允许使用ASCII属性名称。
您需要explicitly define this particular class更改名称:
Base = automap_base()
class RDV(Base):
__tablename__ = "RDV"
...
accepted = Column("accepté", Boolean, ...)