SQLAlchemy将带有重音字符的查询发送到MSSQL

时间:2017-03-06 17:23:53

标签: python sql-server encoding sqlalchemy pymssql

我正在使用
  - Python 2.7.11
  - SQLAlchemy 1.1.3。
  - pymssql 2.1.3

在Windows 10上

我在使用包含特殊字符“à”的特定查询字符串时遇到了一些麻烦。该字符串使用Latin-1编码,因此我以这种方式执行解码

>>> query = "exec CFG.cfg.mystore_proc 'PARAM1', 'PARAM2', 'dbo_società'"
>>> query = query.decode("latin1")
>>> query
u"exec CFG.cfg.mystore_proc  'PARAM1', 'PARAM2', 'dbo_societ\x85'"
>>> query = query.encode("utf8")
>>> query
"exec CFG.cfg.mystore_proc  'PARAM1', 'PARAM2', 'dbo_societ\xc2\x85'"
>>> db.session.execute(query)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\bulonea\projects\mercurial\DWHUI\WebSite\venv\lib\site-packages\sqlalchemy\orm\session.py", line 1101, in execute
clause = expression._literal_as_text(clause)
  File "C:\Users\bulonea\projects\mercurial\DWHUI\WebSite\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 4232, in _literal_as_text
return TextClause(util.text_type(element))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 66: ordinal not in range(128)

我以这种方式创建了引擎

SQLA.create_engine(connection_string, poolclass=poolclass, encoding="utf-8")

0 个答案:

没有答案