在使用cx_oracle模塊讀取Oracle數據庫中的中文記錄時,返回值皆爲?號,後google得此佳文,遂問題得以解決,特於此記之。
======================================================================
oracle數據庫版本是10g,字符集是AL32UTF8.
編寫的python腳本中需要加入如下幾句:
- import os
- os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
這樣可以保證select出來的中文顯示沒有問題。
要能夠正常的insert和update中文,還需要指定python源文件的字符集密碼和oracle一致。
- # -*- coding: utf-8 -*-
例子:
- # -*- coding: utf-8 -*-
- import os
- os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
- import cx_Oracle
- db = cx_Oracle.connect(username/passwd@192.168.2.222:42401/xezf')
- cursor = db.cursor()
- rs = cursor.execute('select * from cfg_haoduan_gh where rownum<9')
- li =rs.fetchall()
- print li[0][3].decode('utf-8')
- cursor.execute('insert into test_ccc values(1,sysdate,\'北\')')
- db.commit()
- db.close()