Oracle基礎:3: 程式中呼叫sqlplus的方式
阿新 • • 發佈:2018-11-10
通過sqlplus可以連線資料庫根據使用者許可權進行資料或者設定操作,但是需要互動操作並返回結果,這篇文章介紹一下如何在程式中使用sqlplus。
環境準備
使用Oracle的精簡版建立docker方式的demo環境,詳細可參看:
Here Document
因為sqlplus是控制檯的方式與使用者進行互動式的輸入/輸出對應,而在程式執行的過程中顯然是需要預先定好的輸入,這樣可以考慮使用Here Document,比如希望通過sqlplus來確認資料庫版本資訊,則可以這樣
# sqlplus system/liumiao123 <<EOF > select * from v\$version; > EOF SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 11:06:42 2018 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production SQL> BANNER -------------------------------------------------------------------------------- Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production PL/SQL Release 11.2.0.2.0 - Production CORE 11.2.0.2.0 Production TNS for Linux: Version 11.2.0.2.0 - Production NLSRTL Version 11.2.0.2.0 - Production SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production #
注意:需要注意v$version中的$需要轉義
建立table
接下來使用Here Document的方式呼叫sqlplus來建立table
# sqlplus system/abcd1234 <<EOF > create table student ( > stuid number(4), > stuname varchar2(50), > primary key (stuid) > ); > desc student; > EOF SQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 11:11:52 2018 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production SQL> 2 3 4 5 Table created. SQL> Name Null? Type ----------------------------------------- -------- ---------------------------- STUID NOT NULL NUMBER(4) STUNAME VARCHAR2(50) SQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production #
總結
sqlplus結合Here Document即可實現在程式中呼叫sqlplus。