-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathORCL_CONN.py
108 lines (79 loc) · 2.55 KB
/
ORCL_CONN.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
# @Author: Boya Chiou
# @Date: 2017-06-13 09:32:35
# @Last Modified by: Boya Chiou
# @Last Modified time: 2017-07-17 19:46:24
import cx_Oracle
import sys
import time
class ORCL_CONN(object):
"""docstring for SchemaTool"""
def __init__(self):
pass
def CONN(self):
dsnStr = cx_Oracle.makedsn("[your server IP]", "[your port]", "[your service name]")
self.conn = cx_Oracle.connect(user="[user name]", password="[password]", dsn=dsnStr)
self.c = self.conn.cursor()
return self.c
def Query(self, TableName):
self.TableName = TableName
sql = """SELECT * FROM {0} WHERE ROWNUM <= 1""".format(self.TableName)
data = self.c.execute(sql)
# for i in data:
# print i
def SchemaInfo(self):
# FOR MY OWN REFERENCE
SchemaInfo = self.c.description
for i in SchemaInfo:
print i
def SchemaExpo(self):
HeaderInfo = self.c.description
breaker = "/*-------------------------------BREAK LINE------------------------------------*/"
with open("{0}.txt".format(self.TableName), 'w') as f:
f.write('name, type, display_size, internal_szie, precision, scale, null_ok\n\n')
f.write("CREATE TABLE {0}\n".format(self.TableName))
f.write("(\n")
for i in HeaderInfo:
Name = '{0} '.format(str(i[0]))
DataType = '{0} '.format(str(i[1]).rsplit('.',1)[1][:-2])
Display_Size = '{0}'.format(str(i[2]))
Precision = '{0}'.format(str(i[4]))
f.write(Name)
if DataType.strip() == "STRING":
f.write("VARCHAR ")
f.write("({0}),\n".format(Display_Size))
elif DataType.strip() == "FIXED_CHAR":
f.write("CHAR ")
f.write("({0}),\n".format(Display_Size))
elif DataType.strip() == "NUMBER":
f.write("NUMERIC ")
f.write("({0}),\n".format(Precision))
elif DataType.strip() == "DATETIME":
f.write("DATETIME,\n")
else:
f.write("None String/Numeric type not difined")
# f.write('{0}\n'.format(str(i)))
# f.write(breaker)
f.write(")")
def Big_Query(self):
sql = """
<please put your query here>
"""
data = self.c.execute(sql)
for i, e in enumerate(data):
print "RECORD #{0}".format(i+1),'\n',e,'\n'
if __name__ == "__main__":
S = ORCL_CONN()
S.CONN()
# table_name = raw_input("Please Enter Table Schema You'd Want to Export (e.g.: SCHEMA_NAME.TABLE_NAME): ")
# S.Query(table_name)
# Enter the table name of the table you'd like to download
#S.Query("TABLE_NAME")
#S.SchemaExpo()
t0 = time.clock()
# S.SchemaInfo()
S.Big_Query()
t1 = time.clock()
print "Process Time: {0}".format(t1-t0)
# Close Connection
S.conn.close()