读书人

cx_Oracle 中文汉字乱码有关问题解决

发布时间: 2012-10-27 10:42:25 作者: rapoo

cx_Oracle 中文汉字乱码问题解决

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

import osos.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/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()

读书人网 >编程

热点推荐