-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSamuele_expJ.py
65 lines (51 loc) · 2.08 KB
/
Samuele_expJ.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
import oracledb as oraDB
import json
from datetime import datetime
import pandas as pd
import subprocess
# Resto del tuo codice...
# Eseguire una query di esempio
def tableExportJson (table, conn):
cursor = conn.cursor()
query = f"SELECT * FROM {table}"
cursor.execute(query)
results = pandas.DataFrame (cursor.fetchall())
# Recuperare i risultati
# results = []
# for row in cursor:
# results.append(row)
# Chiudere il cursore e la connessione
cursor.close()
# Creare il file JSON e salvare i risultati
data = datetime.now().strftime("%Y%m%d")
resultsJSON = results.to_json (f'JSON\{data}_{table}.json')
print (results)
def output_type_handler(cursor, name, default_type, size, precision, scale):
if default_type == oraDB.DB_TYPE_CLOB:
return cursor.var(oraDB.DB_TYPE_LONG, arraysize=cursor.arraysize)
if default_type == oraDB.DB_TYPE_BLOB:
return cursor.var(oraDB.DB_TYPE_LONG_RAW, arraysize=cursor.arraysize)
def _debug_json(table:str, conn:oraDB.Connection):
conn.outputtypehandler = output_type_handler
cursor = conn.cursor()
query = f"SELECT * FROM {table}"
cursor.execute(query)
#results=pandas.read_sql(query,con=conn)
lobResults=cursor.fetchall()
#print(lobResults[4][3])
# Get column names from the cursor description
columns = [col[0] for col in cursor.description]
# Create a DataFrame using the fetched results and column names
df = pd.DataFrame(lobResults, columns=columns)
data = datetime.now().strftime("%Y%m%d")
df.to_json (f'JSON\{data}_{table}.json',orient='records',indent=True)
##subprocess.Popen(r'explorer /select,"\\srvopenco\Open-Co\Personali\03 - Temporanea\Samuele\PythonProject\JSON\"')
cursor.close()
#main "fittizio" per testare direttamente da qui le funzioni
if __name__=='__main__':
import ConnORC_DB
import expJ
connection = ConnORC_DB.openDbConnection('OracleDat.dat')
expJ._debug_json('TABCLSCRIPT',connection)
#expJ._debug_json('TABCCAMPIONI',connection)
ConnORC_DB.closeDbConnection(connection)