Skip to content

Commit 8359261

Browse files
isatyamksccurme
andauthored
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 <chester.curme@gmail.com>
1 parent 035a9c9 commit 8359261

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

cookbook/oracleai_demo.ipynb

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@
8686
"\n",
8787
"import oracledb\n",
8888
"\n",
89-
"# please update with your username, password, hostname and service_name\n",
90-
"# please make sure this user has sufficient privileges to perform all below\n",
89+
"# Update with your username, password, hostname, and service_name\n",
9190
"username = \"\"\n",
9291
"password = \"\"\n",
9392
"dsn = \"\"\n",
@@ -97,40 +96,45 @@
9796
" print(\"Connection successful!\")\n",
9897
"\n",
9998
" cursor = conn.cursor()\n",
100-
" cursor.execute(\n",
101-
" \"\"\"\n",
102-
" begin\n",
103-
" -- drop user\n",
104-
" begin\n",
105-
" execute immediate 'drop user testuser cascade';\n",
106-
" exception\n",
107-
" when others then\n",
108-
" dbms_output.put_line('Error setting up user.');\n",
109-
" end;\n",
110-
" execute immediate 'create user testuser identified by testuser';\n",
111-
" execute immediate 'grant connect, unlimited tablespace, create credential, create procedure, create any index to testuser';\n",
112-
" execute immediate 'create or replace directory DEMO_PY_DIR as ''/scratch/hroy/view_storage/hroy_devstorage/demo/orachain''';\n",
113-
" execute immediate 'grant read, write on directory DEMO_PY_DIR to public';\n",
114-
" execute immediate 'grant create mining model to testuser';\n",
115-
"\n",
116-
" -- network access\n",
117-
" begin\n",
118-
" DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(\n",
119-
" host => '*',\n",
120-
" ace => xs$ace_type(privilege_list => xs$name_list('connect'),\n",
121-
" principal_name => 'testuser',\n",
122-
" principal_type => xs_acl.ptype_db));\n",
123-
" end;\n",
124-
" end;\n",
125-
" \"\"\"\n",
126-
" )\n",
127-
" print(\"User setup done!\")\n",
128-
" cursor.close()\n",
99+
" try:\n",
100+
" cursor.execute(\n",
101+
" \"\"\"\n",
102+
" begin\n",
103+
" -- Drop user\n",
104+
" begin\n",
105+
" execute immediate 'drop user testuser cascade';\n",
106+
" exception\n",
107+
" when others then\n",
108+
" dbms_output.put_line('Error dropping user: ' || SQLERRM);\n",
109+
" end;\n",
110+
" \n",
111+
" -- Create user and grant privileges\n",
112+
" execute immediate 'create user testuser identified by testuser';\n",
113+
" execute immediate 'grant connect, unlimited tablespace, create credential, create procedure, create any index to testuser';\n",
114+
" execute immediate 'create or replace directory DEMO_PY_DIR as ''/scratch/hroy/view_storage/hroy_devstorage/demo/orachain''';\n",
115+
" execute immediate 'grant read, write on directory DEMO_PY_DIR to public';\n",
116+
" execute immediate 'grant create mining model to testuser';\n",
117+
" \n",
118+
" -- Network access\n",
119+
" begin\n",
120+
" DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(\n",
121+
" host => '*',\n",
122+
" ace => xs$ace_type(privilege_list => xs$name_list('connect'),\n",
123+
" principal_name => 'testuser',\n",
124+
" principal_type => xs_acl.ptype_db)\n",
125+
" );\n",
126+
" end;\n",
127+
" end;\n",
128+
" \"\"\"\n",
129+
" )\n",
130+
" print(\"User setup done!\")\n",
131+
" except Exception as e:\n",
132+
" print(f\"User setup failed with error: {e}\")\n",
133+
" finally:\n",
134+
" cursor.close()\n",
129135
" conn.close()\n",
130136
"except Exception as e:\n",
131-
" print(\"User setup failed!\")\n",
132-
" cursor.close()\n",
133-
" conn.close()\n",
137+
" print(f\"Connection failed with error: {e}\")\n",
134138
" sys.exit(1)"
135139
]
136140
},

0 commit comments

Comments
 (0)