SQLAlchemy集成cx_Oracle中文亂碼
問題描述
使用SQLAlchemy集成cx_Oracle後查出的中文數據都是???(也就是說亂碼了)
解決方案
方案一(推薦)
注意SQLAlchemy的版本,此方案需要在1.3以上版本纔有效!
create_conf = "oracle://{user}:{passwd}@{host}:{port}/{db}?encoding=UTF-8&nencoding=UTF-8"
_engine = create_engine(create_conf.format(**self.conf), encoding='utf-8')
方案二 (網上大多數解決方案)
import os
create_conf = "oracle://{user}:{passwd}@{host}:{port}/{db}"
_engine = create_engine(create_conf.format(**self.conf), encoding='utf-8')
os.environ["NLS_LANG"] = "GERMAN_GERMANY.UTF8"