在使用cx_oracle模块读取Oracle数据库中的中文记录时,返回值皆为?号,后google得此佳文,遂问题得以解决,特于此记之。

======================================================================

oracle数据库版本是10g,字符集是AL32UTF8.
编写的python脚本中需要加入如下几句:

 
  
  1. import os 
  2. os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' 

这样可以保证select出来的中文显示没有问题。
要能够正常的insert和update中文,还需要指定python源文件的字符集密码和oracle一致。

 
  
  1. # -*- coding: utf-8 -*- 

例子:

 
  
  1. # -*- coding: utf-8 -*- 
  2.  
  3. import os 
  4. os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' 
  5.  
  6. import cx_Oracle 
  7. db = cx_Oracle.connect(username/passwd@192.168.2.222:42401/xezf') 
  8. cursor = db.cursor() 
  9. rs = cursor.execute('select * from cfg_haoduan_gh where rownum<9'
  10. li =rs.fetchall() 
  11. print li[0][3].decode('utf-8'
  12.  
  13. cursor.execute('insert into test_ccc values(1,sysdate,\'北\')'
  14.  
  15. db.commit() 
  16. db.close()  

原文地址:http://jun-zhou.iteye.com/blog/953073