Welcome, guest | Sign In | My Account | Store | Cart

"desc.py scott/tiger order " will list the column info for table "order". "desc.py scott/tiger@production order " will list the column info for table "order" of tns name "production".

Python, 33 lines
 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
#!/usr/bin/env python
import DCOracle2,sys

def describe(connStr,table): 
 db=None
 try:
  db=DCOracle2.connect(connStr)
  dd=db.describe(table)

  fmt='%-20s\t%-10s\t%-10s' 
  typeFmt='%s(%s)'
  print fmt % ('Name','Null?','Type')
  print fmt % ('-'*20,'-'*10,'-'*10)
  for col in dd['OCI_ATTR_LIST_COLUMNS']:
    nullable=col['OCI_ATTR_IS_NULL']
    null=''
    if (not nullable): null='NOT NULL'
    type=typeFmt % (DCOracle2.Type(col['OCI_ATTR_DATA_TYPE']),col['OCI_ATTR_DATA_SIZE'])
    print fmt % (col['OCI_ATTR_NAME'],null,type)  
 finally:
   if db!=None:
	print 'close db'
   	db.close()

if __name__=='__main__':
 try:
   if (len(sys.argv)==3):
	describe(sys.argv[1],sys.argv[2])	
   else:
	print 'usage: desc.py <connection string> <table name>'
	print '       such as desc.py scott/tiger orders     '
 except:
	print 'Unexpected error:', sys.exc_info()[1]

equivalent to Oracle sqlplus "describe" without the extra output when u run bash$echo "describe order" | sqlplus scott/tiger

Good for used in Bash or any other shell scripting. such as "./desc.py scott/tiger order | cut -f1 | grep -i "^SHIP*" will return all the column names started with "SHIP" from order table