oracle中lob型別的相關操作

2021-05-27 13:48:37 字數 4276 閱讀 5835

在oracle的早期版本中,使用long

和long

raw型別存放大資料。從8i版本開始,oracle引入了lob資料型別,並且oracle建議開發人員盡量去使用lob型別而不去使用

long

和long

raw定位器

是操作這些值的指標。當我們想讀取或者修改lob資料時,我們必須先要獲取lob

定位器,然後通過lob定位器才能讀取或者修改lob資料,下面的示例能夠幫助我們更好的理解lob資料和lob定位器:

declare

piclob blob;

begin

--假定

piccolumn列是blob型別

select piccolumn into piclob from mytable where id=10;

end;

當select into語句執行完之後,

piclob變數將被賦值,但是

);insert into clob_table(id) values(1);  --remark列沒有定位器,並且沒有資料

insert into clob_table values(2,'id為2的備註資訊');  --remark列有定位器,並且有資料

insert into clob_table values(3,empty_clob());--remark含有定位器,但是沒有資料

檢索定位器的值:

declare

mylob clob;

begin

select remark into mylob from clob_table where id=___;

if mylob is null then

dbms_output.put_line('mylob is null');

else

dbms_output.put_line(mylob);

end if;

end;

當下劃線填入1時,結果如下:

mylob is null

當下劃線填入2時,結果如下:--列印出定位器所指向的資料

id為2的備註資訊

當下劃線填入3時,沒有列印出資訊。

修改id為1的remark列,使其具有定位器:

update clob_table set remark=empty_clob() where id=1;

這時再呼叫以上的**塊,這次沒有列印出資訊。

示例2:向clob列追加資料

declare

mylob clob;

buffer varchar2(20) := '這是第一次追加的內容';

amount int;

offset int;

begin

--要想能夠修改lob資料,必須指定for update子句

select remark into mylob from clob_table where id=2 for update;

amount:=length(

buffer

);  --獲取追加文字的長度

offset:=dbms_lob.getlength(mylob)+1;  --獲取定位器引用的lob資料的

長度dbms_lob.write(mylob,amount,offset,

buffer

);--向clob列追加資料,每次最大追加32767位元組。要注意必須先存在定位器才能追加資料,否則會出現invalid lob locator specified: ora-22275錯誤。

buffer

);--使用這個函式不用考慮offset引數

end;

示例3:讀取clob資料

declare

mylob clob;

text varchar2(40) := '這是第三次追加的內容';

amount int;

offset int;

begin

select remark into mylob from clob_table where id=2;

amount:=15;

offset:=3;

--從lob資料中的第三個字元開始讀取共讀取15個字元放入到text變數

dbms_lob.read(mylob,amount,offset,text);

dbms_output.put_line(text);

end;

三、使用bfile資料型別

bfile型別不存放檔案實際資料,而是存放檔案的指標,檔案的資料儲存在作業系統的檔案系統上,並且我們只能通過bfile型別讀取檔案的資料,不能修改。

示例1:建立scottdir目錄,並授予scott使用者讀許可權

create directory scottdir as 'e:scottdir';--注意此語句並不會在作業系統上建立資料夾

grant read on directory scottdir to scott;

示例2:建立含有bfile資料型別的列,並插入資料

create table bfile_table

(id number primary key,

remark  bfile

);插入資料:

insert into bfile_table(id) values(1);

update bfile_table set remark=bfilename('scottdir','1.txt');

insert into bfile_table(id,remark) values(2,bfilename('

scottdir

','2.doc'));

注意:bfile資料型別指向的檔案,即可以是文字檔案也可以是二進位制檔案。而且bfile資料型別不能通過select語句直接檢視其內容。

示例3:將bfile檔案的內容讀取到clob列(前提是bfile檔案是文字檔案)

declare

src   bfile;

des   clob;

amount    int;

src_offset int :=1;

des_offset int :=1;

csid int :=850;

lc   int :=0;

warning int;

begin

select remark into src from bfile_table where id=1;

select remark into des from clob_table where id=1 for update;

dbms_lob.fileopen(src,dbms_lob.file_readonly);

amount:=dbms_lob.getlength(src);

dbms_output.put_line(amount);

dbms_lob.loadclobfromfile(des,src,amount,des_offset,src_offset,csid,lc,warning);

dbms_lob.close(src);

end;

四、使用blob資料型別

示例1:建立帶有blob列的表並插入資料

create table blob_table

(id number(3),

remark blob

);insert into blob_table values(1,empty_blob());

將檔案內容插入到blob列:

declare

src   bfile;

des   blob;

amount    int;

src_offset int :=1;

des_offset int :=1;

csid int :=0;

lc   int :=0;

warning int;

begin

src:=bfilename('scottdir','2.doc');

select remark into des from blob_table where id=1 for update;

dbms_lob.fileopen(src,dbms_lob.file_readonly);

amount:=dbms_lob.getlength(src);

dbms_output.put_line(amount);

--dbms_lob.loadblobfromfile(des,src,amount,des_offset,src_offset);

dbms_lob.close(src);

end;

Oarcle中的LOB型別

1 在oracle中,lob large object,大型物件 型別的字段現在用得越來越多了。因為這種型別的字段,容量大 最多能容納4gb的資料 且乙個表中可以有多個這種型別的字段,很靈活,適用於資料量非常大的業務領域 如圖象 檔案等 2 lob型別分為blob和clob兩種 blob即二進位制大...

Oracle中的LOB欄位解讀

lob欄位是oracle資料庫用於儲存大資料物件的字段型別,包括blob clob nlob bfile 當lob欄位大小超過4k時,資料庫會單獨為該lob欄位分配額外的blob segments儲存blob物件,儲存在lobsegment中的lob預設不在緩衝區快取,對於lob的讀寫都是物理io,...

ORACLE中LOB欄位的使用和維護

隨著社會的發展,在現代資訊系統的開發中,需要儲存的已不僅僅是簡單的文字資訊,同時還包括一些和音像資料或者是超長的文字。比如開發一套旅遊資訊系統,每乙個景點都有豐富的 音像資料和大量的文字介紹。這就要求後台資料庫要有儲存這些資料的能力。oracle公司在其oracle8i中通過提供lob欄位實現了該功...