|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "433df165-49d5-4a00-904a-103df8480d92", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Oracle AI Data Platform v1.0\n", |
| 9 | + "\n", |
| 10 | + "Copyright © 2025, Oracle and/or its affiliates.\n", |
| 11 | + "\n", |
| 12 | + "Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "id": "2ad176b5-9ec9-4c97-80e3-751c08fb23be", |
| 18 | + "metadata": {}, |
| 19 | + "source": [ |
| 20 | + "# Execute Oracle SQL on Oracle ADW\n", |
| 21 | + "\n", |
| 22 | + "## Prerequisites\n", |
| 23 | + "1. install oracledb in your cluster (create requirements.txt and add oracledb package)\n", |
| 24 | + "2. upload your tnsnames.ora and ewallet.pem from your wallet into the workspace\n", |
| 25 | + "\n", |
| 26 | + "## Overview\n", |
| 27 | + "\n", |
| 28 | + "This example defines a function you can reuse, it generates a sample SQL query and executes it." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "id": "9ddb1eeb-ae80-4c36-acea-259361859c9b", |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "# Define the Oracle function execute_oracle_sql to execute simple SQL statements on Oracle ADW\n", |
| 39 | + "#\n", |
| 40 | + "# Parameters:\n", |
| 41 | + "# - v_sql_file_path: path to Oracle SQL file in workspace or volume\n", |
| 42 | + "# - v_config_dir: directry where your tnsnames.ora resides\n", |
| 43 | + "# - v_wallet_dir: directory where your ewallet.pem resides\n", |
| 44 | + "# - v_user: Oracle database user\n", |
| 45 | + "# - v_password: Oracle database user password\n", |
| 46 | + "# - v_dsn: Oracle DSN\n", |
| 47 | + "# - v_wallet_password: password fow wallet\n", |
| 48 | + "# - fail_on_error: True/False if statement fails, setting to True will stop the script execution, otherwise it till continue\n", |
| 49 | + "#\n", |
| 50 | + "import oracledb\n", |
| 51 | + " \n", |
| 52 | + "def execute_oracle_sql(v_sql_file_path, v_config_dir, v_wallet_dir, v_user, v_password, v_dsn, v_wallet_password, fail_on_error=False):\n", |
| 53 | + " # Read the SQL file\n", |
| 54 | + " sql_script=\"\"\n", |
| 55 | + " with open(v_sql_file_path, 'r') as file:\n", |
| 56 | + " sql_script = file.read()\n", |
| 57 | + " \n", |
| 58 | + " # Split the script into individual statements (naive split by semicolon)\n", |
| 59 | + " statements = [stmt.strip() for stmt in sql_script.split(';') if stmt.strip()]\n", |
| 60 | + " try:\n", |
| 61 | + " # Connect to Oracle\n", |
| 62 | + " with oracledb.connect(\n", |
| 63 | + " config_dir=v_config_dir,\n", |
| 64 | + " user=v_user,\n", |
| 65 | + " password=v_password,\n", |
| 66 | + " dsn=v_dsn,\n", |
| 67 | + " wallet_location=v_wallet_dir,\n", |
| 68 | + " wallet_password=v_wallet_password) as connection:\n", |
| 69 | + " with connection.cursor() as cursor:\n", |
| 70 | + " for statement in statements:\n", |
| 71 | + " print(f\"Executing: {statement}\")\n", |
| 72 | + " try:\n", |
| 73 | + " cursor.execute(statement)\n", |
| 74 | + " except Exception as e:\n", |
| 75 | + " if (fail_on_error):\n", |
| 76 | + " raise e;\n", |
| 77 | + " else:\n", |
| 78 | + " print(\" Statement failed (but continuing):\", e)\n", |
| 79 | + " connection.commit()\n", |
| 80 | + " print(\"SQL script executed successfully.\")\n", |
| 81 | + " except Exception as e:\n", |
| 82 | + " print(\"Error:\", e)\n" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "markdown", |
| 87 | + "id": "c9b33030-4657-4fed-a7e5-18a273faa26b", |
| 88 | + "metadata": {}, |
| 89 | + "source": [ |
| 90 | + "## Generate Sample SQL script\n", |
| 91 | + "End each statement with a ; it can be multi-line, no PLSQL blocks supported as simplified parsing used. It can also be DDL to create tables." |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "id": "22b19f1a-76a2-4695-8d24-9b917346241e", |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "%%writefile /Workspace/my_oracle_script.sql\n", |
| 102 | + "select \"Connected\" msg from dual;\n", |
| 103 | + "select \"Connected\" msg from dual;" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "markdown", |
| 108 | + "id": "88013578-513b-44a7-b87a-c6bc8ff6ad00", |
| 109 | + "metadata": {}, |
| 110 | + "source": [ |
| 111 | + "## Execute Sample SQL Script\n", |
| 112 | + "\n", |
| 113 | + "Update the variables below with your values." |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": null, |
| 119 | + "id": "c0d09e31-ac66-4d2b-bf03-64c5c815b509", |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "sql_file_path = \"/Workspace/my_oracle_script.sql\"\n", |
| 124 | + "config_dir=\"/Workspace/your_folder_location_for_tns_names_ora\",\n", |
| 125 | + "user=\"your_user\",\n", |
| 126 | + "password=\"\",\n", |
| 127 | + "dsn=\"your_tns_alias\",\n", |
| 128 | + "wallet_location=\"/Workspace/your_folder_location_for_wallet_pem\",\n", |
| 129 | + "wallet_password=\"\"\n", |
| 130 | + " \n", |
| 131 | + "execute_oracle_sql(sql_file_path, config_dir, wallet_location, user, password, dsn, wallet_password)\n" |
| 132 | + ] |
| 133 | + } |
| 134 | + ], |
| 135 | + "metadata": { |
| 136 | + "kernelspec": { |
| 137 | + "display_name": "Python 3 (ipykernel)", |
| 138 | + "language": "python", |
| 139 | + "name": "python3" |
| 140 | + }, |
| 141 | + "language_info": { |
| 142 | + "codemirror_mode": { |
| 143 | + "name": "ipython", |
| 144 | + "version": 3 |
| 145 | + }, |
| 146 | + "file_extension": ".py", |
| 147 | + "mimetype": "text/x-python", |
| 148 | + "name": "python", |
| 149 | + "nbconvert_exporter": "python", |
| 150 | + "pygments_lexer": "ipython3", |
| 151 | + "version": "3.9.12" |
| 152 | + } |
| 153 | + }, |
| 154 | + "nbformat": 4, |
| 155 | + "nbformat_minor": 5 |
| 156 | +} |
0 commit comments