通過sqlplus可以連接數據庫根據用戶權限進行數據或者設定操作,但是需要交互操作并返回結果,這篇文章介紹一下如何在程序中使用sqlplus。
環境準備
使用Oracle的精簡版創建docker方式的demo環境,詳細可參看:
Here Document
因為sqlplus是控制臺的方式與用戶進行交互式的輸入/輸出對應,而在程序執行的過程中顯然是需要預先定好的輸入,這樣可以考慮使用Here Document,比如希望通過sqlplus來確認數據庫版本信息,則可以這樣
# sqlplus system/liumiao123 <<EOF> select * from v/$version;> EOFSQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 11:06:42 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> BANNER--------------------------------------------------------------------------------Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionPL/SQL Release 11.2.0.2.0 - ProductionCORE 11.2.0.2.0 ProductionTNS for Linux: Version 11.2.0.2.0 - ProductionNLSRTL Version 11.2.0.2.0 - ProductionSQL> Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production#
注意:需要注意v$version中的$需要轉義
創建table
接下來使用Here Document的方式調用sqlplus來創建table
# sqlplus system/liumiao123 <<EOF> create table student (> stuid number(4),> stuname varchar2(50),> primary key (stuid)> );> desc student;> EOFSQL*Plus: Release 11.2.0.2.0 Production on Sun Oct 21 11:11:52 2018Copyright (c) 1982, 2011, Oracle. All rights reserved.Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit ProductionSQL> 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。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VeVb武林網的支持。
新聞熱點
疑難解答