ora_sql.py




import cx_Oracle
import sys,os

print os.getcew()

class easyOra(object):
### initial class parameters
 def __init__(self,loginAcc,loginPassword,connString):
  self.acc=str(loginAcc)
  self.psd=str(loginPassword)
  self.connstr=str(connString)
  try:
   conn = cx_Oracle.connect((self.acc)+"/"+(self.psd)+"@"+(self.connstr))
   conn.close()
  except:
   print 'ORA-01017: invalid username/password; logon denied'
   print 'please input correct user/password or TNS'
   sys.exit()
   
 def exeQry(self,sqlString):
  conn = cx_Oracle.connect((self.acc)+"/"+(self.psd)+"@"+(self.connstr)) 
  cursor = conn.cursor()
  cursor.execute(str(sqlString))  
  row = cursor.fetchall() 
  for x in row:
   print x
  cursor.close
  conn.close()
 def spool(self,sqlString,outputfile,delimiter):
  outf=open(outputfile,'w')
  conn = cx_Oracle.connect((self.acc)+"/"+(self.psd)+"@"+(self.connstr)) 
  cursor = conn.cursor()
  cursor.execute(str(sqlString))  
  row = cursor.fetchall() 
  for x in row:
   for y in x:
    if (str(y)=='None'):
     outf.write(delimiter)
    else:
     outf.write(str(y)+delimiter)
   outf.write('\n')
  cursor.close
  conn.close()
  
sql=easyOra('Username','Password','Serversid')

sql.exeQry('select * from table where rownum<=10')

sql.spool('select * from user_tables where rownum<=20','user_tables.txt',' | ')

你可能感兴趣的:(Python)