Skip to content

Commit af5b01a

Browse files
uncommenting the stored procedure function as this will be used in C3. Adding a function to update/insert data in a table
1 parent 5c96fec commit af5b01a

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

utils/oracle.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,40 @@ def execute_query(self, query: str,): # To use when "select xxxx" (stored proced
9494
except Exception as connectionError:
9595
print(f"Failed to connect to the DB! with connection error {connectionError}")
9696

97-
# The following function is commented out as it is not used currently, but may be needed in future compartments
97+
def execute_stored_procedure(self, procedure: str): # To use when "exec xxxx" (stored procedures)
98+
try:
99+
print("Attempting DB connection...")
100+
conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
101+
print(conn.version, "DB connection successful!")
102+
try:
103+
print(f"Attempting to execute stored procedure: {procedure}")
104+
cursor = conn.cursor()
105+
cursor.callproc(procedure)
106+
conn.commit()
107+
print(conn.version, "stored procedure execution successful!")
108+
except Exception as executionError:
109+
print(f"Failed to execute stored procedure with execution error: {executionError}")
110+
finally:
111+
if conn is not None:
112+
conn.close()
113+
except Exception as connectionError:
114+
print(f"Failed to connect to the DB! with connection error: {connectionError}")
98115

99-
# def execute_stored_procedure(self, procedure: str): # To use when "exec xxxx" (stored procedures)
100-
# try:
101-
# print("Attempting DB connection...")
102-
# conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
103-
# print(conn.version, "DB connection successful!")
104-
# try:
105-
# print(f"Attempting to execute stored procedure: {procedure}")
106-
# cursor = conn.cursor()
107-
# cursor.callproc(procedure)
108-
# print(conn.version, "stored procedure execution successful!")
109-
# except Exception as executionError:
110-
# print(f"Failed to execute stored procedure with execution error: {executionError}")
111-
# finally:
112-
# if conn is not None:
113-
# conn.close()
114-
# except Exception as connectionError:
115-
# print(f"Failed to connect to the DB! with connection error: {connectionError}")
116+
def update_or_insert_data_to_table(self, statement): # To update or insert data into a table
117+
try:
118+
print("Attempting DB connection...")
119+
conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns)
120+
print(conn.version, "DB connection successful!")
121+
try:
122+
print("Attempting to insert/update table")
123+
cursor = conn.cursor()
124+
cursor.execute(statement)
125+
conn.commit()
126+
print(conn.version, "DB table successfully updated!")
127+
except Exception as dbUpdateInsertError:
128+
print(f"Failed to insert/update values from the DB table! with error {dbUpdateInsertError}")
129+
finally:
130+
if conn is not None:
131+
conn.close()
132+
except Exception as connectionError:
133+
print(f"Failed to connect to the DB! with connection error {connectionError}")

0 commit comments

Comments
 (0)