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
| import cx_Oracle import pymysql import os import time
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' source_db = pymysql.connect(host="127.0.0.1",user="root",password="root",database="mydb") target_db = cx_Oracle.connect('scott/oracle@127.0.0.1/orcl') source_db_type='MySQL' target_db_type='Oracle' cur_select = source_db.cursor() cur_insert = target_db.cursor() cur_select.arraysize = 500 cur_insert.arraysize=500 source_table=input("请输入源表名称:") target_table=input("请输入目标表名称:") if source_db_type.upper()=='ORACLE': get_column_length='select * from '+source_table+' where rownum<=1' elif source_db_type.upper()=='MYSQL': get_column_length='select * from ' + source_table + ' limit 1' cur_select.execute(get_column_length) col_len=len(cur_select.fetchone()) val_str = '' if target_db_type.upper()=='MYSQL': for i in range (1,col_len): val_str=val_str+'%s'+',' val_str=val_str+'%s' elif target_db_type.upper()=='ORACLE': for i in range (1,col_len): val_str=val_str+':'+str(i)+',' val_str=val_str+':'+str(col_len) insert_sql='insert into '+target_table+' values('+val_str+')' select_sql='select * from '+source_table cur_select.execute(select_sql) print('开始执行:',time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) while True: rows=list(cur_select.fetchmany(500)) cur_insert.executemany(insert_sql, rows) target_db.commit() if not rows: break cur_select.close() cur_insert.close() source_db.close() target_db.close() print('执行成功:',time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
|