-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_sql_launcher.py
More file actions
43 lines (35 loc) · 1.27 KB
/
python_sql_launcher.py
File metadata and controls
43 lines (35 loc) · 1.27 KB
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
#Import libraries:
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.sql import text
import logging
import sys
import csv
#Connect to the database (Oracle):
connection_string = "oracle+cx_oracle://" + sys.argv[1] + ":" + sys.argv[2] + "@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + sys.argv[3] + ")(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=" + sys.argv[4] + ")(SERVER=DEDICATED)))"
engine = create_engine(connection_string)
#Stream logging on CLI, level "error"
handler = logging.StreamHandler()
handler.setLevel(logging.ERROR)
logging.getLogger('sqlalchemy.engine').addHandler(handler)
#Launch PL/SQL and/or SQL query
with engine.connect() as con:
rs = con.execute(sys.argv[5])
results = rs.all()[1:100000]
#Print the num of elements and all the elements in the CLI
number_of_elements = len(results)
print("Number of elements in the list: ", number_of_elements)
for x in range(number_of_elements):
a = str(x)
print("List element in position: " + a)
print(results[x])
#Opening the csv file in 'w+' mode
file = open('results.csv', 'w+', newline ='')
# writing the data into the file
with file:
write = csv.writer(file)
for x in range(number_of_elements):
data = [results[x]]
write.writerows(data)
#Exit Python3
exit()