[python] convert Oracle table to Mysql

Jameskrauser Lee
1 min readJun 13, 2018

--

step1.
Open SQL Developer to export table schema.
save sql to oracleSource.sql.

#oracleSource.sqlCREATE TABLE "DBName"."tablename"(  
"aaa" VARCHAR2(10 BYTE),
"bbb" NUMBER(1,0),"ccc" DATE,"ddd" DATE) SEGMENT CREATION IMMEDIATE

step2. run python
cmd : python ConvertSQL.py

#coding:utf-8'''date: 20180613version: 1.0convert Oracle SQL to MySQLpython version : 2.7
filename : ConvertSQL.py
execute command: python ConvertSQL.py
'''import osdef main():print("- - - Convert Oracle SQL to MySQL - - - ")source_file = 'oracleSource.sql'save_file = 'Mysqlfile.sql'file_path = "./" + source_fileprint ("read file from " + source_file )f = open( source_file, 'r')result = list()for line in f.readlines():line = line.strip()line = line.replace("\"", "`" )line = line.replace("NUMBER","DECIMAL")line = line.replace("VARCHAR2","VARCHAR")line = line.replace("BYTE","")if not len(line) or line.startswith('#'):continueelif line.startswith(')'):break#print (line)result.append(line)result.append(");")#print(result)if ( os.path.isfile(file_path)):print("File exists! remove it ")os.remove(file_path)else:print("File not found!")open(save_file , 'w').write('%s' % '\n'.join(result) )print ("save data to " + save_file)print("- - - Program Stop - - - ")

MySQL
step3. you can open MySQLWorkBench to run it SQL to create Table.

CREATE TABLE `DBName`.`tablename`
(
`aaa` VARCHAR(10 ),
`bbb` DECIMAL(1,0),
`ccc` DATE,
`ddd` DATE
);

all example code
https://bitbucket.org/jameskrauser/convertoracletomysql/downloads/

--

--

Jameskrauser Lee
Jameskrauser Lee

Written by Jameskrauser Lee

For the last few years. i was involved mostly in the development of Automatic Fare collection system for the Chennai Metro. Familiar with C++ and iOS.

No responses yet