-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_sql.py
75 lines (57 loc) · 1.76 KB
/
insert_sql.py
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
# -*- coding: utf-8 -*-
import codecs
import cx_Oracle
def main(fn, fn2):
print("start")
con = cx_Oracle.connect('dbuser/dbpw@ORCL')
print(con.version)
cur = con.cursor()
#x = cur.fetchall()
fw = codecs.open(fn2, "wb", 'utf-8')
with codecs.open(fn, "rb", 'utf-8') as fh:
i = 0
j = 0
sql = ""
while True:
#print(".",end="")
i = i +1
x = fh.readline()
if i<3:
#remove REM and SET line
continue
### if i>40:
### break
if i % 100 == 0:
cur.execute("commit")
if len(x) == 0:
break
#print(repr(x))
sql = sql + x
if sql[-4:] == ');\r\n':
#print(sql)
j = j + 1
#print("%d." % j)
try:
#print(sql[:-3])
v = sql[:-3].replace('\n',"'||chr(10)||'")
cur.execute(v)
#print(v)
#print("done")
except cx_Oracle.IntegrityError as exi:
#print(exi)
pass
except Exception as ex:
#print(">>", end=":")
#print(ex)
fw.write(sql[:-3].replace('\n',"'||chr(10)||'")+";\r\n")
#print(sql.encode('utf8'))
#break
sql = ""
#print(">==========================")
#break
fw.close()
print("done")
if __name__ == "__main__":
fn = "t1.sql"
fn2 = "t1-1.sql"
main(fn, fn2)