在oracle中有个大对象(lobs)类型可用分别是blobclobbfilenclob
下面是对lob数据类型的简单介绍
blob:二进制lob为二进制数据最长可达GB存贮在数据库中
clob:字符lob字符数据最长可以达到GB存贮在数据库中
bfile:二进制文件;存贮在数据库之外的只读型二进制数据最大长度由操作系统限制
nclob支持对字节字符集合(nultibyte characterset)的一个clob列
对于如何检索和操作这些lob数据一直是oracle数据库开发者经常碰到的问题下面我将在oracle对lob数据处理的一些方法和技巧介绍给读者希望能够对读者以后的开发有所帮助
oracle中可以用多种方法来检索或操作lob数据通常的处理方法是通过dbms_lob包
其他的方法包括使用api(application programming interfaces)应用程序接口和oci(oracle call interface)oracle调用接口程序
一在oracle开发环境中我们可以用dbms_lob包来处理!
dbms_lob包功能强大简单应用既可以用来读取内部的lob对象也可以用来处理bfile对象但处理两者之间还有一点差别处理内部lob对象(blobclob)时可以进行读和写但处理外部lob对象bfile时只能进行读操作写的操作可以用pl/sql处理另外用sql也可以处理lob但要注意sql仅可以处理整个lob不能操作lob的数据片
在dbms_lob包中内建了read()appendwrite()erase()copy()getlength()substr()等函数可以很方便地操作lob对象这里不做深入讨论读者可以参看相关的书籍
对于pl/sql下面介绍一种技巧用动态的pl/sql语句处理clob对象来传替表名!
example
动态PL/SQL对CLOB字段操作可传递表名table_name表的唯一标志字段名field_idclob字段名field_name记录号v_id开始处理字符的位置v_pos传入的字符串变量v_clob
修改CLOB的PL/SQL过程updateclob
create or replace procedure updateclob(
table_name in varchar
field_id in varchar
field_name in varchar
v_id in number
v_pos in number
v_clob in varchar)
is
lobloc clob;
c_clob varchar();
amt binary_integer;
pos binary_integer;
query_str varchar();
begin
pos:=v_pos*+;
amt := length(v_clob);
c_clob:=v_clob;
query_str :=select ||field_name|| from ||table_name|| where ||field_id||= :id for update ;
initialize buffer with data to be inserted or updated
EXECUTE IMMEDIATE query_str INTO lobloc USING v_id;
from pos position write varchar into lobloc
dbms_lobwrite(lobloc amt pos c_clob);
commit;
exception
when others then
rollback;
end;
l /用法说明
在插入或修改以前先把其它字段插入或修改CLOB字段设置为空empty_clob()
然后调用以上的过程插入大于到个字符
如果需要插入大于个字符编一个循环即可解决问题
查询CLOB的PL/SQL函数getclob
create or replace function getclob(
table_name in varchar
field_id in varchar
field_name in varchar
v_id in number
v_pos in number) return varchar
is
lobloc clob;
buffer varchar();
amount number := ;
offset number := ;
query_str varchar();
begin
query_str :=select ||field_name|| from ||table_name|| where ||field_id||= :id ;
initialize buffer with data to be found
EXECUTE IMMEDIATE query_str INTO lobloc USING v_id;
offset:=offset+(v_pos)*;
read varchar from the buffer
dbms_lobread(loblocamountoffsetbuffer);
return buffer;
exception
when no_data_found then
return buffer;
end;
l 用法说明
用select getclob(table_namefield_idfield_namev_idv_pos) as partstr from dual;
可以从CLOB字段中取个字符到partstr中
编一个循环可以把partstr组合成dbms_lobgetlength(field_name)长度的目标字符串
二对于在其他不同的开发环境例如vcvbpbjava等环境下对lob的处理处理方法不尽相同在这里将简要举几个例子来说明不在oracle开发环境下对lob的处理
(一) 在pb中的处理
exampler
string ls_pathls_filenamels_jhdh
long ll_numll_countrtn
blob ole_blob
ll_num=dw_lbgetrow()
if ll_num> then ls_jhdh=dw_lbobjectct_njhdh[ll_num]
select count(*) into :ll_count from sj_jh_jhfjb where ct_jhdlxbh= and ct_jhdh=:ls_jhdh and ct_jdlxbh=:is_jdlx;
if ll_count> then
rtn=messagebox(提示是否要修改此附件question!yesno!)
if rtn= then
SELECTBLOB ct_jhfjnr INTO le_blob from sj_jh_jhfjb where ct_jhdlxbh= and ct_jhdh=:ls_jhdh and ct_jdlxbh=:is_jdlx;
ole_objectdata =ole_blob
If ole_activate(offsite!) <> Then
Messagebox(OLE Activate不能激活)
Return
end If
end if
else
messagebox(提示没有附件)
end if
end if
(二)在vb中的处理
在vb中处理大对象一般可以用OOO(oracle objects for ole)来处理大对象这里介绍一种不用处理大对象blob的方法
下面这段程序可以将一个文件(文本文件doc文件图象文件等)保存到数据库中并可以将其从数据库读出
需要两个commandbutton
cmd 名称 cmdsave caption 保存
cmd 名称 cmdread caption 读取
一个cmddialog控件
同时需要创建一张表t_demo(字段id 类型 number;字段text 类型 blob;)
exmple
Option Explicit
Dim rn As ADODBConnection
Public Function CreateDataSource(DataSource As String UserID As String Password As String) As Boolean
On Error GoTo DbConErr:
Set rn = New ADODBConnection
With rn
ConnectionString = Provider=OraOledbOracle; & _
password= & Password & ; & _
User ID = & UserID & ; & _
Data Source= & DataSource & ; & _
Locale Identifier=
Open
End With
CreateDataSource = True
Exit Function
DbConErr:
CreateDataSource = False
End Function
Private Sub cmdRead_Click()
Dim rs As New ADODBRecordset
rsActiveConnection = rn
rsLockType = adLockOptimistic
rsCursorLocation = adUseClient
rsSource = select * from t_demo
rsOpen
ComDlgDirDialogTitle = 保存文件
ComDlgDirFilter = **
ComDlgDirShowSave
Call BlobToFile(rsFields(text) ComDlgDirfilename)
Set rs = Nothing
Exit Sub
Set rs = Nothing
End Sub
Private Sub cmdsave_Click()
Dim rs As New ADODBRecordset
rsActiveConnection = rn
rsLockType = adLockOptimistic
rsCursorLocation = adUseClient
rsSource = select * from t_demo
rsOpen
rsAddNew
ComDlgDirDialogTitle = 选取文件
ComDlgDirShowOpen
rsFields(id)Value =
If ComDlgDirfilename <> Then
Call FileToBlob(rsFields(text) ComDlgDirfilename)
rsUpdate
End If
Set rs = Nothing
Exit Sub
Set rs = Nothing
End Sub
Private Sub Form_Load()
If Not CreateDataSource(sid systemp manager) Then
MsgBox Connection failure!
End If
End Sub
fld As ADODBField filename As String Optional ChunkSize As Long = )
Dim fnum As Integer bytesleft As Long bytes As Long
Dim tmp() As Byte
If (fldAttributes And adFldLong) = Then
ErrRaise field doesnt support the GetChunk method
End If
If Dir$(filename) = Then ErrRaise File not found
fnum = FreeFile
Open filename For Binary As fnum
bytesleft = LOF(fnum)
Do While bytesleft
bytes = bytesle