insert clob field by 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
import cx_Oracle, os

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

def main():
dsn_tns = cx_Oracle.makedsn('server', 1521, service_name='xxrac')
con = cx_Oracle.connect('user','password',dsn_tns)

try:
cur = con.cursor()
for i in range(50):
report_id = '%LSA' + str(i).rjust(3, '0') + '%'
#print report_id
cur.execute("SELECT rpt_url from print_para where rpt_url like '%s' ORDER BY last_update desc" %(report_id))
row = cur.fetchone()
if row != None:
print 'http://xxserver' + row[0]

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

def insert_news():
dsn_tns = cx_Oracle.makedsn('server', 1521, service_name='xxrac')
con = cx_Oracle.connect('user2','user2uat0675',dsn_tns)

dhprd_dsn_tns = cx_Oracle.makedsn('server2', 1521, 'prd')
dhprd_con = cx_Oracle.connect('user2x','password',prd_dsn_tns)

try:
dhprd_cur = dhprd_con.cursor()
dhprd_cur.execute("SELECT NEWS_ID,GROUP_ID,CREATION_DATE,PUBLISH_START_DATE,PUBLISH_END_DATE,NEWS_TITLE,NEWS_TITLE_CHI,STATUS,LAST_UPDATE,UPDATE_BY,UPDATE_ROLE,NEWS_TYPE,URL,URL_CHI,URL_SIMP_CHI,PRESS_RELEASE, NEWS_CONTENT from news ")
res = dhprd_cur.fetchall()

for row in res:
cur = con.cursor()
print row[0]
NEWS_ID = row[0]
GROUP_ID = row[1]
CREATION_DATE = row[2]
PUBLISH_START_DATE = row[3]
PUBLISH_END_DATE = row[4]
NEWS_TITLE = row[5]
NEWS_TITLE_CHI = row[6]
STATUS = row[7]
LAST_UPDATE = row[8]
UPDATE_BY = row[9]
UPDATE_ROLE = row[10]
NEWS_TYPE = row[11]
URL = row[12]
URL_CHI = row[13]
URL_SIMP_CHI = row[14]
PRESS_RELEASE = row[15]
NEWS_CONTENT = row[16]

#cur.execute("insert into news(NEWS_ID,GROUP_ID,CREATION_DATE,PUBLISH_START_DATE,PUBLISH_END_DATE,NEWS_TITLE,NEWS_TITLE_CHI,STATUS,LAST_UPDATE,UPDATE_BY,UPDATE_ROLE,NEWS_TYPE,URL,URL_CHI,URL_SIMP_CHI,PRESS_RELEASE) values (:NEWS_ID,:GROUP_ID,:CREATION_DATE,:PUBLISH_START_DATE,:PUBLISH_END_DATE,:NEWS_TITLE,:NEWS_TITLE_CHI,:STATUS,:LAST_UPDATE,:UPDATE_BY,:UPDATE_ROLE,:NEWS_TYPE,:URL,:URL_CHI,:URL_SIMP_CHI,:PRESS_RELEASE)", {'NEWS_ID' : NEWS_ID,'GROUP_ID' : GROUP_ID,'CREATION_DATE' : CREATION_DATE,'PUBLISH_START_DATE' : PUBLISH_START_DATE,'PUBLISH_END_DATE' : PUBLISH_END_DATE,'NEWS_TITLE' : NEWS_TITLE,'NEWS_TITLE_CHI' : NEWS_TITLE_CHI,'STATUS' : STATUS,'LAST_UPDATE' : LAST_UPDATE,'UPDATE_BY' : UPDATE_BY,'UPDATE_ROLE' : UPDATE_ROLE,'NEWS_TYPE' : NEWS_TYPE,'URL' : URL,'URL_CHI' : URL_CHI,'URL_SIMP_CHI' : URL_SIMP_CHI,'PRESS_RELEASE' : PRESS_RELEASE})

#cur.execute("update news set NEWS_CONTENT = :NEWS_CONTENT, NEWS_CONTENT_CHI = :NEWS_CONTENT_CHI where news_id = :news_id", {'news_id':NEWS_ID, 'NEWS_CONTENT':'aa', 'NEWS_CONTENT_CHI':'bb'})

#cur.execute("select NEWS_CONTENT from news where news_id = :news_id for update", {'news_id':NEWS_ID})
print NEWS_CONTENT.read()
'''row2, = cur.fetchone()
print NEWS_CONTENT.read()
row2.write(NEWS_CONTENT.read())'''
#con.commit()
cur.close()
break

dhprd_cur.close()

finally:
print 'close connection'
con.close()
dhprd_con.close()



def insert_news_content():
dsn_tns = cx_Oracle.makedsn('server', 1521, service_name='xxrac')
con = cx_Oracle.connect('user2','user2uat0675',dsn_tns)

dhprd_dsn_tns = cx_Oracle.makedsn('server2', 1521, 'prd')
dhprd_con = cx_Oracle.connect('user2','password',dhprd_dsn_tns)

try:
dhprd_cur = dhprd_con.cursor()
dhprd_cur.execute("SELECT NEWS_ID from news order by NEWS_ID")
res = dhprd_cur.fetchall()
news_ids = []
for row in res:
news_ids.append(row[0])


for NEWS_ID in news_ids:
dhprd_cur.execute("SELECT NEWS_ID, NEWS_CONTENT, NEWS_CONTENT_CHI from news where NEWS_ID = :NEWS_ID", {'NEWS_ID':NEWS_ID})
row = dhprd_cur.fetchone()

NEWS_ID = row[0]
NEWS_CONTENT = row[1].read()
NEWS_CONTENT_CHI = row[2].read()

cur = con.cursor()

print NEWS_ID
cur.execute('select news_content, news_content_chi from news where news_id = :news_id for update', {'news_id':NEWS_ID})
c1, c2 = cur.fetchone()
c1.open()
c1.trim()
c1.write(NEWS_CONTENT)
c1.close()

c2.open()
c2.trim()
c2.write(NEWS_CONTENT_CHI)
c2.close()

con.commit()
cur.close()
dhprd_cur.close()

finally:
print 'close connection'
con.close()
dhprd_con.close()

insert_news_content()