-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbconvert.py
212 lines (203 loc) · 7.13 KB
/
dbconvert.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#-*- coding:utf8 -*- -
import os
import sys
from sys import argv
import time
import cx_Oracle
import MySQLdb
from warnings import filterwarnings
class pylog():
def __init__(self):
self.success = 'success.log'
self.fail = 'fail.log'
self.wr = 'wr.log'
def logsuccess(self,s):
with open(self.success,'a') as f:
f.write('%s\n') % str(s)
def logfail(self,s):
with open(self.self.fail,'a') as f:
f.write('%s\n') % s
def logwr(self,s):
with open(self.self.wr,'a') as f:
f.write('%s\n') % s
class colour_output():
def red(self,s):
print '\033[31;40m%s\033[0m' % s
def green(self,s):
print '\033[32;40m%s\033[0m' % s
def str2timetuple(date):
#时间转换函数
time_format = '%Y-%m-%d %H:%M:%S'
return datetime.strptime(date, time_format)
def conver(table):
#sql = 'select * from "%s" ' % table
sql = 'select * from %s ' % table
try:
row_list = oracle.sqlSelect(sql)
except Exception ,e:
co.red('------------------------------------------------')
co.red('|--TAB Output--| %s' % table)
co.red('|--SQL Output--| %s' % sql)
co.red('|--TAB FAIL --| Error : %s' % e)
co.red('------------------------------------------------')
#exit()
return 0
desclist = []
for desc in row_list[0]:
desclist.append(desc[0])
dataLen = len(row_list[1])
co.green('------------------------------------------------')
co.green('|--TAB Output--| %s' % table)
co.green('|--SQL Output--| %s' % sql)
co.green('|--TAB INFO --| dataLen: %d' % dataLen)
co.green('------------------------------------------------')
cycle_count = 0
success = 0
fail = 0
keyword_list = []
for row in row_list[1]:
rowkv = dict(zip(desclist,row))
for k, v in rowkv.items():
if len(keyword_list) > 0:
if k in keyword_list:
rowkv['`%s`' % k] = rowkv.pop(k)
k = '`%s`' % k
else :
pass
else :
rowkv['`%s`' % k] = rowkv.pop(k)
k = '`%s`' % k
if not v:
rowkv.pop(k)
elif type(v) =='datetime.datetime':
rowkv[k] = str(str2timetuple(v)).decode('GBK').encode('UTF-8')
#rowkv[k] = str(str2timetuple(v))
else :
try :
rowkv[k] = str(v).decode('GBK').encode('UTF-8')
#rowkv[k] = v
except Exception,e:
rowkv[k] = v
#exit(0)
cycle_count += 1
qmarks = ', '.join(['%s'] * len(rowkv))
cols = ', '.join(rowkv.keys())
#sql = 'INSERT INTO `%s` (%s) VALUES (%s)' % (table, cols, qmarks)
sql = 'INSERT INTO %s (%s) VALUES (%s)' % (table, cols, qmarks)
#print sql,tuple(rowkv.values())
#sof = 'success'
sof = mysql.insert(sql,tuple(rowkv.values()))
if sof == 'success':
co.green('table %s row %d success , left %d' %(table,cycle_count,dataLen-cycle_count))
success += 1
else :
co.red('table %s row %d FAIL: %s , left %d' % (table,cycle_count,str(sof),dataLen-cycle_count))
fail += 1
#return 1
co.green('TABLE: %s Done , SUCCESS %d , FAIL %d' %(table,success,fail))
print '\n'
return 1
class mbdc():
def __init__(self):
self.connection = MySQLdb.connect(host='127.0.0.1',port=3306,user='phs',passwd='phs',db='phs3_test',charset='utf8')
self.cursor = self.connection.cursor()
def insert(self, query, params):
try:
self.cursor.execute(query, params)
self.connection.commit()
return 'success'
#except MySQLdb.Warning, w:
#return str(w)
except Exception, e:
self.connection.rollback()
return e
def query(self, query, params):
cursor = self.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(query, params)
return cursor.fetchall()
def select(self, sql):
cursor = self.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(sql)
return cursor.fetchall()
def version(self):
cursor = self.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT VERSION()")
ver = cursor.fetchone()
print 'MySQL database version: %s' % ver['VERSION()']
return ver
def __del__(self):
self.connection.close()
class odbc():
def __init__(self):
connstr = 'phsconvert/[email protected]:1521/orcl'
#self.db=cx_Oracle.connect(connstr,encoding = "UTF-8", nencoding = "UTF-8")
self.db=cx_Oracle.connect(connstr)
print 'Oracle database version: %s'% self.db.version
def sqlSelect(self,sql):
cr=self.db.cursor()
cr.execute(sql)
retdesc = cr.description
returnstr=cr.fetchall()
cr.close()
return retdesc,returnstr
def __del__(self):
self.db.close()
if __name__=='__main__':
reload(sys)
sys.setdefaultencoding('utf8')
os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.ZHS16GBK'
#filterwarnings('error', category = MySQLdb.Warning)
print '--------------------init--------------------'
oracle = odbc()
mysql = mbdc()
mysql.version()
co = colour_output()
pl = pylog()
try:
table_list_str = argv[1]
co.green('Get Arg %s' % table_list_str)
except:
co.green('NO Arg read tablist')
tablist = open('tablist')
try:
table_list_str = tablist.read()
except Exception ,e:
co.red('Open tablist Fail : %s') % str(e)
finally:
tablist.close()
table_list = table_list_str.replace('\n','').split(',')
success_list = []
fail_list = []
tables_success = 0
tables_fail = 0
print '--------------------------------------------\n\n\n'
time.sleep(1)
#os.system('clear')
time.sleep(1)
try :
for table in table_list:
co.green('convert %s ...' % table)
if (conver(table)):
tables_success += 1
success_list.append(table)
else :
tables_fail += 1
fail_list.append(table)
#exit()
co.green('\n\n\n--------------------DONE--------------------')
print ''
co.green('DB convert SUCCESS , SUCCESS : %d , FAIL : %d' % (tables_success,tables_fail))
print ''
co.green('--------------------------------------------\n')
co.green('SCCESS tables : %s' % str(success_list))
print '\n'
co.red('FAIL tables : %s' % str(fail_list))
except Exception,e :
co.red('\n\n\n--------------------%s--------------------' % str(e))
print ''
co.red('DB convert ERROR , SUCCESS : %d , FAIL : %d' % (tables_success,tables_fail))
print ''
co.red('--------------------------------------------\n')
co.green('SCCESS tables : %s' % str(success_list))
print '\n'
co.red('FAIL tables : %s' % str(fail_list))