cx_Oracle 中文汉字乱码问题解决

写一个python应用,在开头声明了编码 #-*-coding:utf-8-*- 可是插入数据的时候,依然乱码。后来查了好多资料,终于找到了办法。在程序的头部加入:

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

 能够正常的insert和update中文

例子:

# -*- coding: utf-8 -*-

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

import cx_Oracle
db = cx_Oracle.connect(username/[email protected]: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()

你可能感兴趣的:(oracle)