问题描述
我正在使用 UTF-16
编码的Oracle数据库。直接使用cx_oracle客户端时,变音符号正确显示。连接语句是这样的:
I'm using Oracle Database with UTF-16
encoding. The diacritics is correctly displayed when using directly cx_oracle client. Connection statement is like this:
cx_Oracle.connect(username, password, conn_str, encoding='UTF-16', nencoding='UTF-16')
但是,现在我正在构建更大的应用程序,我想使用 Flask
中的 SQLalchemy
。
However, now I'm building bigger application and I would like to use SQLalchemy
in Flask
.
代码如下:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
db.Model.metadata.reflect(db.engine)
class MyTable(db.Model):
__table__ = db.Model.metadata.tables['mytable']
for row in MyTable.query:
print(row.column_with_diacritics)
代码输出上图: aoe
但是数据库中的列值为:áóé
所以我的问题是,如何将参数 encoding ='UTF-16',nencoding ='UTF-16'
传递给sqlalchemy使用的cx_oracle
So my question is, how can pass arguments encoding='UTF-16', nencoding='UTF-16'
to cx_oracle which sqlalchemy uses underhood?
谢谢您的任何建议或其他解决方法。
Thank you for any advice or other workaround.
推荐答案
我发现可以通过环境变量 NLS_LANG
。
I have found out that it's possible to pass this setting via environment variable NLS_LANG
.
因此,在我的用例中,我必须设置: NLS_LANG = .AL32UTF8
(工作而不是UTF16)。
So, in my use case, I had to set: NLS_LANG=.AL32UTF8
(worked instead of UTF16).
参考:
这篇关于将编码参数从sqlalchemy传递给cx_oracle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!