In auditing, it is sometimes necessary to modify the table structure of Oracle and then create the table structure in SQL Server, and then import the data into SQL Server, in the process of modifying the table structure. Manual modification, the most stupid method, or using the tool UE batch modification, still do the method, The most convenient way is to use a script swish, all changed well:
VI Parameter.ini
#sysType---1:linux;others:windows
Systype=1
#readFiledir is OldFile
Readfiledir=/root/tmpmycnf/dbquery/yoon/oldfile
#writeFiledir is New File
Writefiledir=/root/tmpmycnf/dbquery/yoon/newfile
VI sql.py
#! /usr/bin/python
#-*-Coding:utf-8-*-
#Author: Yoon
#Version: 1.0
#Date: 2014-07-07
Import OS
import sys
 
class Application (object):
def __init__ (self):
self._oldfiledir = '
Self._newfiledir = '
self._inifilename = './parameter.ini '
Self._start = ' false '
def readinitfile (self,keyname):
data = open (Self._inifilename, ' R ')
For line in data:
if keyName = = line.split (' = ') [0].strip ():
return line.split (' = ') [1].strip ()
Break
Data.close ()
def getnewline (self,oldline):
If Oldline.strip (). StartsWith (' CREATE TABLE '):
TableName = Oldline.strip (). Split ('. ') [1].strip ()
NewLine = ' CREATE TABLE ' + tablename + oldline[-1]
Else
If Oldline.strip (). StartsWith (' ('):
NewLine = Oldline
Elif oldline.startswith (') '):
newline = ' null ' + oldline[-1] + oldline + oldline[-1]
Self._start = ' false '
Else
Strlist = Oldline.split (")
newline = ' '
For string in Strlist:
if string = = ':
NewLine + = "
Elif String.strip (). Upper (). StartsWith (' DEFAULT '):
Continue
Elif (string.upper () = = ' not ') or (string.upper () = = ' NULL '):
Continue
Elif String.upper (). StartsWith (' VARCHAR2 '):
newline + = ' varchar (' + str (int (string[string.find (' (') +1:string.find (') ')]) + ') '
Elif (string.upper () = = ' number ') or (string.upper () = = ' DATE ') or (string.upper () = = ' DATETIME ') or (string.upper () = = ' T Imestamp (6) ') or (string.upper () = = ' INTEGER '):
newline + = ' varchar (100) '
Elif ((String.upper (). StartsWith (' number ')) or (String.upper (). StartsWith (' DATE ')) or (String.upper (). StartsWith (' DATETIME ')) or (String.upper (). StartsWith (' TIMESTAMP (6) ') or (String.upper (). StartsWith (' INTEGER '))) and (string[-1 ] = = ' \ n '):
newline + = ' varchar (100) '
Elif (String.upper (). StartsWith (' number, ')) or (String.upper (). StartsWith (' DATE, ')) or (String.upper (). StartsWith (' DATETIME, ')) or (String.upper (). StartsWith (' TIMESTAMP (6), ')) or ((String.upper (). StartsWith (' number (')) and (', ' in String) or ((String.upper (). StartsWith (' number (')) and (', ' in string)) or (String.upper (). StartsWith (' INTEGER, ')):
newline + = ' varchar (+) NULL '
Elif String.upper (). StartsWith (' Number ('):
newline + = ' varchar (100) '
Else
i = 0
while (1 = = 1):
If strlist[i] = = ":
i + = 1
Else
Filedname=strlist[i]
Break
if string = = Filedname:
NewLine = string + newline
If ', ' in string:
newline + = ' null ' +string[string.rfind (', '):]
Return newline
def createnewfilefromoldfilelist (self):
Readfiledir = Self.readinitfile (' Readfiledir ')
Writefiledir = Self.readinitfile (' Writefiledir ')
Systype = Self.readinitfile (' Systype ')
if Systype = = ' 1 ':
Sepstr = '/'
Else
sepstr = ' \ \ '
Listfile=os.listdir (Readfiledir)
For file in ListFile:
Start = ' false '
WriteFile = open (Writefiledir+sepstr+file, ' W ')
ReadData = open (Readfiledir+sepstr+file, ' R ')
For line in ReadData:
If Line.lower (). StartsWith (' CREATE TABLE '):
Self._start = ' true '
If self._start== ' true ':
NewLine = Self.getnewline (line)
Writefile.write (NewLine)
Readdata.close ()
Writefile.close ()
App = Application ()
App.createnewfilefromoldfilelist ()
[Email protected] ~]#
[Email protected] ~]#/usr/bin/python sql.py
Oracle table Structure transformation SQL Server table structure script