jxdiscuz 发表于 2017-5-4 08:14:33

python 查数据库,汉字是 \x 编码

环境:python2.6
oracle:GBK
查询数据库汉字出现:\xbd\xf0\xbc\xd1\xb0\xdb\xd2\xb5 这种貌似是8进制的代码

#
# 查询 SQL
# 返回数据数组
#
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.ZHS16GBK' # linux 中文
def selectSQL(sql):
try:
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
conn = cx_Oracle.connect("counter123", "counter456", dsn_tns)
cursor = conn.cursor()
cursor.execute(sql)
r = cursor.fetchall()
finally:
cursor.close()
conn.close()
return r


# -*- coding:GBK
'''
Created on 2010-9-6
@author: DOC
'''
from com.tmg.utils import DBUtils

pArgs = []
def getProductIdAndName():
sql = 'select id, productname from mv_product_id where rownum < 20'
#    sql = 'select name from count_page_201007 where rownum < 20'
return DBUtils.selectSQL(sql)
pArgs = getProductIdAndName()
for i in pArgs:
print i
解决了:
# 元组会自动编码、解码汉字:()
a = ('m', '工')
print a
print a
页: [1]
查看完整版本: python 查数据库,汉字是 \x 编码