我的第一个python实用程序[续1]
上次,我是从数据库中取出数据存入文件后,用python处理. 这次直接用python连数据库处理^^
---------------------------------------------
#Filename: format_string5.py
# -*- coding: utf-8 -*-
import cx_Oracle
# tab_prcpln string5字段, 依次为 idd, 来电显示 010101210 , ...
subsvc = ['010101103', '010101210', '010101221', '010101240', '010101250', '010101914', '010101340', '010101341', '010101905']
#open file to write sth
f = file('prcpln.txt', 'w')
#连接数据库
conn = cx_Oracle.connect('aaa/bbb@ccc')
curs = conn.cursor()
#取出相应套餐id,及各子业务的价格. 注:格子业务加个放在字段string5中, 用'/'分割.
sql = "select a.prcplnid||','||replace(a.string5, '/', ',') from tab_prcpln a where enddate > sysdate"
rr = curs.execute(sql)
rows = curs.fetchall()
#插入tf_b_prcpln_string5的sql语句
sql_insert = """INSERT INTO TF_B_PRCPLN_STRING5 VALUES (:prcplnid,:seq,:subsvcid,:fee)"""
for ee in rows:
ss = list(ee)
#获得一行记录
for line in ss:
xx = line.split(',', 1)
prcplnid = xx[0]
vv = xx[1].split(',')
for i in range(len(vv)):
seq = repr(i)
subsvcid = subsvc
fee = vv
ans = prcplnid + ',' +seq + ',' + subsvcid +',' + fee
#将数据备份在文件中
f.write(ans + '\n')
#将此记录插入表tf_b_prcpln_string5中
bindVars = {'prcplnid':prcplnid, 'seq':seq, 'subsvcid':subsvcid, 'fee':fee}
curs.execute(sql_insert, bindVars)
#close file
f.close()
curs.close()
conn.commit()
print 'OK'