Pass encoding parameter to cx_oracle from sqlalchemy

强颜欢笑 提交于 2019-12-05 18:43:59

SQLAlchemy create_engine allows you to pass additional arguments to the underlying cx_Oracle's connect() via the connect_args argument to create_engine:

import cx_Oracle
e = create_engine(
    "oracle+cx_oracle://user:pass@connstr...",
    connect_args={
        "encoding": "UTF-16",
        "nencoding": "UTF-16"
    }
)

Setting NLS_LANG actually did not work for me, but I think that's because I'm in Cygwin and have some other quirks (such as I actually want to set encoding dynamically, and I'd have to reload cx_Oracle even if I did manage to get NLS_LANG to work). Using this method worked for me.

Source: (SQLAlchemy Oracle dialect source code): https://github.com/zzzeek/sqlalchemy/blob/560452acd292c8a9a57db032378a6342f16448c6/lib/sqlalchemy/dialects/oracle/cx_oracle.py#L37

I have found out that it's possible to pass this setting via environment variable NLS_LANG.

So, in my use case, I had to set: NLS_LANG=.AL32UTF8 (worked instead of UTF16).

Reference: cx_oracle Documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!