Oracle的Blob字段比較特殊,他比long字段的性能要好不少,能夠用來保存例如圖片之類的二進制數據。sql
/* conn scott/tiger; Create TableSpace ts5_21 DataFile 'E:\Oracle\ts5_21.dbf' Size 5m; */ Create Table tLob ( no Number(4), name VarChar2(10), resume CLob, photo BLob, record BFile )
Lob (resume,photo)Store As ( Tablespace ts5_21 Chunk 6k Disable Storage In Row );
(Tablespace ts5_21 --指定存儲的表空間 Chunk 6k --指定數據塊大小)數據庫
/* insert into tlob values(1,'Gene','CLOB大對象列',empty_blob(),bfilename('MYDIR','IMG_0210.JPG'));
insert into tlob values(2,'Jack','CLOB大對象列',empty_blob(),bfilename('MYDIR','IMG_0210.JPG'));
insert into tlob values(3,'Mary','大對象列CLOB',empty_blob(),bfilename('MYDIR','IMG_0210.JPG')); */
1,讀取大對象數據的過程和函數緩存
①:DBMS_LOB.Read():從LOB數據中讀取指定長度數據到緩衝區的過程。 DBMS_LOB.Read(LOB數據,指定長度,起始位置,存儲返回LOB類型值變量); 例子: 網絡
Declare varC clob; vRStr varchar2(1000); ln number(4); Strt number(4); Begin select resume into varC from tlob where no = 1; ln := DBMS_LOB.GetLength(varC); Strt := 1; DBMS_LOB.Read(varC, ln, Strt, vRStr); DBMS_output.put_line('Return: '||vRStr); End;
②:DBMS_LOB.SubStr():從LOB數據中提取子字符串的函數。 DBMS_LOB.SubStr(LOB數據,指定提取長度,提取起始位置): 例子: 分佈式
Declare
varC clob;
vRStr varchar2(1000);
ln number(4);
Strt number(4);
Begin
select resume into varC from tlob where no = 1;
ln := 4;
Strt := 1;
vRStr := DBMS_LOB.SubStr(varC, ln, Strt);
DBMS_output.put_line('結果爲: '||vRStr);
End;
③:DBMS_LOB.InStr():從LOB數據中查找子字符串位置的函數。 DBMS_LOB.InStr(LOB數據, 子字符串); 例子: 函數
Declare
varC clob;
vSubStr varchar2(1000);
vRStr varchar2(1000);
ln number(4);
Begin
select resume into varC from tlob where no = 1;
vSubStr := '大對象';
ln := DBMS_LOB.InStr(varC,vSubStr);
DBMS_output.put_line('位置爲: '||ln); vRStr := DBMS_LOB.SubStr(varC, Length(vSubStr), ln);
DBMS_output.put_line('位置爲'||ln||'長度爲'||Length(vSubStr)||'的子字符串爲:'||vRStr);
End;
④:DBMS_LOB.GetLength():返回指定LOB數據的長度的函數。 DBMS_LOB.GetLength(LOB數據);性能
⑤:DBMS_LOB.Compare():比較二個大對象是否相等。返回數值0爲相等,-1爲不相等。 DBMS_LOB.Compare(LOB數據,LOB數據); 例子: 測試
Declare
varC1 clob;
varC2 clob;
varC3 clob;
ln number(4);
Begin
select resume into varC1 from tlob where no = 1;
select resume into varC2 from tlob where no = 2;
select resume into varC3 from tlob where no = 3;
ln := DBMS_LOB.Compare(varC1,varC1);
DBMS_output.put_line('比較的結果爲: '||ln);
ln := DBMS_LOB.Compare(varC2,varC3);
DBMS_output.put_line('比較的結果爲: '||ln);
End;
2,操做大對象數據的過程 操做會改變數據庫中原有數據,須要加上Updata鎖鎖上指定數據列,修改完後提交事務。
①:DBMS_LOB.Write():將指定數量的數據寫入LOB的過程。 DBMS_LOB.Write(被寫入LOB, 寫入長度(指寫入LOB數據),寫入起始位置(指被寫入LOB),寫入LOB數據); 例子: spa
Declare
varC clob;
vWStr varchar2(1000);
vStrt number(4);
ln number(4);
Begin
vWStr := 'CLOB';
ln := Length(vWStr);
vStrt := 5;
select resume into varC from tlob where no = 1 FOR UPDATE; DBMS_LOB.Write(varC, ln, vStrt, vWStr);
DBMS_output.put_line('改寫結果爲: '||varC);
Commit;
End;
②:DBMS_LOB.Append():將指定的LOB數據追加到指定的LOB數據後的過程。 DBMS_LOB.Append(LOB數據,LOB數據); 例子: 操作系統
Declare
varC clob;
vAStr varchar2(1000);
Begin
vAStr := ',這是大對象列';
select resume into varC from tlob where no = 1 FOR UPDATE;
DBMS_LOB.Append(varC, vAStr); commit;
DBMS_output.put_line('追加結果爲: '||varC);
End;
③:DBMS_LOB.Erase():刪除LOB數據中指定位置的部分數據的過程; DBMS_LOB.Erase(LOB數據,指定刪除長度, 開始刪除位置); 例子:
Declare
varC clob;
ln number(4);
strt number(4);
Begin
ln := 1;
strt := 5;
select resume into varC from tlob where no = 1 FOR UPDATE;
DBMS_LOB.Erase(varC, ln, strt);
commit;
DBMS_output.put_line('擦除結果爲: '||varC);
End;
④:DBMS_LOB.Trim():截斷LOB數據中從第一位置開始指定長度的部分數據的過程; DBMS_LOB.Trim(LOB數據,截斷長度); 例子:
Declare
varC clob;
ln number(4);
Begin
ln := 4;
select resume into varC from tlob where no = 1 FOR UPDATE;
DBMS_LOB.Trim(varC, ln);
COMMIT; DBMS_output.put_line('截斷結果爲: '||varC);
End;
⑤:DBMS_LOB.Copy():從指定位置開始將源LOB複製到目標LOB; DBMS_LOB.Copy(源LOB,目標LOB,複製源LOB長度,複製到目標LOB開始位置,複製源LOB開始位置) 例子:
Declare
vDEST_LOB clob; vSRC_LOB clob;
AMOUNT number; DEST_OFFSET number;
SRC_OFFSET number;
Begin
select resume into vDEST_LOB from tlob where no = 1 FOR UPDATE;
select resume into vSRC_LOB from tlob where no = 2 ;
AMOUNT := DBMS_LOB.GetLength(vSRC_LOB);
DEST_OFFSET := DBMS_LOB.GetLength(vDEST_LOB)+1;
SRC_OFFSET := 1; DBMS_LOB.Copy(vDEST_LOB, vSRC_LOB, AMOUNT, DEST_OFFSET, SRC_OFFSET);
DBMS_output.put_line('拷貝結果爲: '||vDEST_LOB);
End;