通過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/liumiao123 <<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。
總結
以上就是Oracle數據庫基礎:程序中調用sqlplus的方式的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值。
分享到:
投訴收藏