`
jun.zhou
  • 浏览: 17192 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

cx_Oracle 中文乱码问题解决

阅读更多
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()
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics