From 835926153b96a5a5d2ac616ca9059528553de25c Mon Sep 17 00:00:00 2001 From: Satyam Kumar Date: Fri, 7 Jun 2024 01:59:24 +0530 Subject: [PATCH] updated oracleai_demo.ipynb (#22635) The outer try/except block handles connection errors, and the inner try/except block handles SQL execution errors, providing detailed error messages for both. try: conn = oracledb.connect(user=username, password=password, dsn=dsn) print("Connection successful!") cursor = conn.cursor() try: cursor.execute( """ begin -- Drop user begin execute immediate 'drop user testuser cascade'; exception when others then dbms_output.put_line('Error dropping user: ' || SQLERRM); end; --------- Co-authored-by: Chester Curme --- cookbook/oracleai_demo.ipynb | 72 +++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/cookbook/oracleai_demo.ipynb b/cookbook/oracleai_demo.ipynb index 8d67e122833..b3254c1962a 100644 --- a/cookbook/oracleai_demo.ipynb +++ b/cookbook/oracleai_demo.ipynb @@ -86,8 +86,7 @@ "\n", "import oracledb\n", "\n", - "# please update with your username, password, hostname and service_name\n", - "# please make sure this user has sufficient privileges to perform all below\n", + "# Update with your username, password, hostname, and service_name\n", "username = \"\"\n", "password = \"\"\n", "dsn = \"\"\n", @@ -97,40 +96,45 @@ " print(\"Connection successful!\")\n", "\n", " cursor = conn.cursor()\n", - " cursor.execute(\n", - " \"\"\"\n", - " begin\n", - " -- drop user\n", - " begin\n", - " execute immediate 'drop user testuser cascade';\n", - " exception\n", - " when others then\n", - " dbms_output.put_line('Error setting up user.');\n", - " end;\n", - " execute immediate 'create user testuser identified by testuser';\n", - " execute immediate 'grant connect, unlimited tablespace, create credential, create procedure, create any index to testuser';\n", - " execute immediate 'create or replace directory DEMO_PY_DIR as ''/scratch/hroy/view_storage/hroy_devstorage/demo/orachain''';\n", - " execute immediate 'grant read, write on directory DEMO_PY_DIR to public';\n", - " execute immediate 'grant create mining model to testuser';\n", - "\n", - " -- network access\n", - " begin\n", - " DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(\n", - " host => '*',\n", - " ace => xs$ace_type(privilege_list => xs$name_list('connect'),\n", - " principal_name => 'testuser',\n", - " principal_type => xs_acl.ptype_db));\n", - " end;\n", - " end;\n", - " \"\"\"\n", - " )\n", - " print(\"User setup done!\")\n", - " cursor.close()\n", + " try:\n", + " cursor.execute(\n", + " \"\"\"\n", + " begin\n", + " -- Drop user\n", + " begin\n", + " execute immediate 'drop user testuser cascade';\n", + " exception\n", + " when others then\n", + " dbms_output.put_line('Error dropping user: ' || SQLERRM);\n", + " end;\n", + " \n", + " -- Create user and grant privileges\n", + " execute immediate 'create user testuser identified by testuser';\n", + " execute immediate 'grant connect, unlimited tablespace, create credential, create procedure, create any index to testuser';\n", + " execute immediate 'create or replace directory DEMO_PY_DIR as ''/scratch/hroy/view_storage/hroy_devstorage/demo/orachain''';\n", + " execute immediate 'grant read, write on directory DEMO_PY_DIR to public';\n", + " execute immediate 'grant create mining model to testuser';\n", + " \n", + " -- Network access\n", + " begin\n", + " DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(\n", + " host => '*',\n", + " ace => xs$ace_type(privilege_list => xs$name_list('connect'),\n", + " principal_name => 'testuser',\n", + " principal_type => xs_acl.ptype_db)\n", + " );\n", + " end;\n", + " end;\n", + " \"\"\"\n", + " )\n", + " print(\"User setup done!\")\n", + " except Exception as e:\n", + " print(f\"User setup failed with error: {e}\")\n", + " finally:\n", + " cursor.close()\n", " conn.close()\n", "except Exception as e:\n", - " print(\"User setup failed!\")\n", - " cursor.close()\n", - " conn.close()\n", + " print(f\"Connection failed with error: {e}\")\n", " sys.exit(1)" ] },