using cx_Oracle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import cx_Oracle, os

os.environ["NLS_LANG"] = "TRADITIONAL CHINESE_HONG KONG.AL32UTF8"

def trim(str):
if str == None:
return ''

def trim2(strs):
list = []
for i in strs:
if i == None:
list.append('')
else:
list.append(i)
return list

def generateSql(table, fields):
sql = 'insert into %s(%s) values (' %(table, ','.join(fields.split(',')) )
sql += ','.join(map(lambda x: ':' + x , fields.split(','))) + ')'
return sql

def generateValues(fields, row):
values = {}
f = fields.split(',')
for i in range(len(f)):
values[f[i]] = row[i]
return values

def district(cur, extCur):
return ('district', 'MODULE,CAT_ID,CODE,NAME,DIST_CODE,LAST_UPDATE,UPDATE_BY,UPDATE_ROLE,WAIT_COUNT,INSPECTION_DIST_CODE')


def misc(cur, extCur):
return ('misc', 'NAME,CODE,DESCR,REMARK,UPDATE_ROLE,WAIT_COUNT,LAST_UPDATE,UPDATE_BY,DD,MM,YEAR,PERIOD,TYPE,REMARK1,DENY_RANK,DURATION_PARAM,REPORT_ID,LIC_TYPE_PARAM,DUMMY_PROMPT,DESCR2')


def main():
os.environ["NLS_LANG"] = "TRADITIONAL CHINESE_HONG KONG.AL32UTF8"
dsn_tns = cx_Oracle.makedsn('server', 1521, service_name='service-name')

con = cx_Oracle.connect('dhsms','useruat2621',dsn_tns)
extCon = cx_Oracle.connect('dhsms_int','useruat0675',dsn_tns)

cur = con.cursor()
extCur = extCon.cursor()
try:
table, fields = task()
sql = 'select %s from %s' %(','.join(fields.split(',')), table)
print sql
cur.execute(sql)
res = cur.fetchall()
for row in res:
extCur.execute(generateSql(table, fields), generateValues(fields, row))

extCon.commit()

cur.close()
extCur.close()
except cx_Oracle.DatabaseError, exc:
print exc
error, = exc.args
print error.message
finally:
print 'close connection'
con.close()
extCon.close()

def updateCompanyPublish():
uat_dsn_tns = cx_Oracle.makedsn('server', 1521, service_name='uat')
prd_dsn_tns = cx_Oracle.makedsn('server-2', 1521, service_name='prd')

uat_con = cx_Oracle.connect('user','password',uat_dsn_tns)
prd_con = cx_Oracle.connect('user','password',prd_dsn_tns)

uat_cur = uat_con.cursor()
prd_cur = prd_con.cursor()

try:
prd_cur.execute("select bus_name, bus_name_chn, unit, FLOOR, BLOCK, bldg, st_no, st_name, area_code, brc, brc_br_code from company where brc in ('12813641','05566198','14304442','09774317','17347013','10161559','04052247','10197423','02410516','06508541','19231608','18405151','05528392','08239275','02577649','08593841','06534261','07286166','01491507','07665609','02718104','04502925','37614254','07919620','EXEMPT07','10492160','13343992','02545054','02725127','04795495','16191407','00039087','04452570','05419513','18766876','01244344','05134767','00559994')")
for row in prd_cur:
print row
uat_cur.execute('update company_publish set bus_name=:bus_name,bus_name_chn=:bus_name_chn,unit=:unit,floor=:floor,block=:block,bldg=:bldg,st_no=:st_no,st_name=:st_name,area_code =:area_code where brc = :brc and brc_br_code = :brc_br_code', {'bus_name':row[0], 'bus_name_chn':row[0],'bus_name_chn':row[1],'unit':row[2],'floor':row[3],'block':row[4],'bldg':row[5],'st_no':row[6],'st_name':row[7],'area_code':row[8],'brc':row[9],'brc_br_code':row[10]})

prd_cur.close()
uat_cur.close()
uat_con.commit()

finally:
uat_con.close()
prd_con.close()


#updateCompanyPublish()


def upgradeSeq():
dsn_tns = cx_Oracle.makedsn('server', 1521, service_name='service-name')


extCon = cx_Oracle.connect('user','password',dsn_tns)
extCur = extCon.cursor()
for i in range(10000):
extCur.execute('select IE_DRUG_ID_SQ.NEXTVAL FROM DUAL')
extCur.close()
extCon.close()


def updateAddress():
f = open('advert_address.txt')
c = f.read()
f.close()
os.environ["NLS_LANG"] = "TRADITIONAL CHINESE_HONG KONG.AL32UTF8"
try:
dsn_tns = cx_Oracle.makedsn('server-2', 1521, 'sid')
con = cx_Oracle.connect('user','password',dsn_tns)
cur = con.cursor()


for line in c.splitlines():
parts = line.split('\t')
seq_no = parts[0]
source_name = parts[1]
publisher = parts[2]
address = parts[3]
fax = parts[4]
tel = parts[5]
print seq_no, source_name, publisher, address
sql = 'update advert_address set source_name = :source_name, publisher = :publisher, address = :address, fax = :fax, tel = :tel where seq_no = :seq_no'
cur.execute(sql, {'seq_no':seq_no, 'source_name':source_name, 'publisher':publisher, 'address':address, 'fax':fax, 'tel':tel})
cur.close()
con.commit()
finally:
print 'close connection'
con.close()


def updateCompanyName():
f = open('ML data.txt')
c = f.read()
f.close()

try:
dsn_tns = cx_Oracle.makedsn('server', 1521, 'psuat')
con = cx_Oracle.connect('user','password',dsn_tns)
cur = con.cursor()

for line in c.splitlines():
parts = line.split('\t')
name = parts[2]
file_no = parts[3]
if file_no != '':
print name, file_no
sql = 'update company_publish set bus_name_chn = :name where brc = (select brc from lic_app where file_no = :file_no)'
cur.execute(sql, {'name':name, 'file_no':file_no})

sql = 'update company set bus_name_chn = :name where brc = (select brc from lic_app where file_no = :file_no)'
cur.execute(sql, {'name':name, 'file_no':file_no})

cur.close()
con.commit()
finally:
print 'close connection'
con.close()


def updateUMAProdGroup():
f = open('uma_group.txt')
c = f.read()
f.close()

try:
dsn_tns = cx_Oracle.makedsn('server-2', 1521, 'sid')
con = cx_Oracle.connect('user','password',dsn_tns)
cur = con.cursor()

for line in c.splitlines():
parts = line.split('\t')
name = parts[0]
code = parts[1]
descr = parts[2]
if name != '':
print name, code, descr

sql = 'insert into misc(name, code, descr) values (:name, :code, :descr)'
cur.execute(sql, {'name':name, 'code':code, 'descr':descr})

cur.close()
con.commit()
finally:
print 'close connection'
con.close()

main()
#updateUMAProdGroup()