ITPub博客

首页 > 数据库 > Oracle > Oracle数据库blob与字符串类型互换方法

Oracle数据库blob与字符串类型互换方法

Oracle 作者:xuanchao000 时间:2014-03-08 11:08:27 0 删除 编辑
1、新建一张测试表
create table tb_test 
(
    id number,
    blb blob
);
commit;
2、往tb_test表中插入一条新记录用于测试。
insert into tb_test values(1, empty_blob());
COMMIT;
注:往有blob类型的字段的数据表中插入新记录,不能直接填入值,必须先往blob字段插入一个empty_blob(),然后再用DBMS_LOB.WRITE函数写入blob的值。
3、向id为1的记录的blb字段写入以下字符串:'你好啊!';
declare 
    directions blob;
    
    amount binary_integer;
    offset integer;
    first_direction varchar2(100);
    more_directions varchar2(500);
begin
    update tb_test set blb = empty_blob() where id = 1;      --更新和新增一样要将BLOB字段设置为EMPTY_BLOB()
    select blb into directions from tb_test where id = 1 for update; --一定要用for update锁住记录,否则DBMS_LOB.OPEN会出错
    DBMS_LOB.OPEN(directions, DBMS_LOB.LOB_READWRITE);
    first_direction := '你好啊!';
    amount := LENGTHB(first_direction);  --number of characters to write有中文必须用LENGTHB
       offset := 1; --begin writing to the first character of the clob
        DBMS_LOB.WRITE(directions, amount, offset, UTL_RAW.cast_to_raw(first_direction));
        --UTL_RAW.cast_to_raw函数将字符串转换成二进制数
    DBMS_LOB.CLOSE(directions);
    commit;
end; 
4、把插入的BLOB数据转换成字符串显示的方式是
select id,UTL_RAW.cast_to_varchar2(blb) blb from tb_test;
 
5、这种方式在显示纯文本字符串时显示的是正常的,可当我插入的数据例如是 这种时在查询显示时就会显示为空。
select * from tb_test;


<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24266146/viewspace-1116756/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-07-19