ORACLE大对象存储
--创建有大对象字段的一张表
create table test001
(
fname varchar2(50),
content blob
)
select * from dba_users;
select * from test001
--(一)..准备插入大对象
--1. 创建文件存放目录(让Oracle管理,该目录)
create or replace directory test_dir
as
'e:\Pictures';
--2.可以将该目录授权给其他用户访问
grant read,write on directory test_dir to scott;
--(二).准备将大对象,存放在test001表中
declare
tempimg blob;--定义临时变量存放数据
tempdir bfile := bfilename('TEST_DIR','Azul.jpg');--非常重要:所有数据都是大写存放的
begin
insert into test001 values ('first.jpg',empty_blob()) returning content into tempimg;
--使用内置的包,给tempimg写入数据
dbms_lob.fileopen(tempdir);--打开指定文件
dbms_lob.loadfromfile(tempimg,tempdir,dbms_lob.getlength(tempdir));
dbms_lob.fileclose(tempdir);--关闭文件
dbms_output.put_line('恭喜你,终于成功了!!!');
commit;
end ;
select * from test001
select * from dba_directories
--将Blob对象,写成磁盘文件
declare
l_file utl_file.file_type;--定义写入磁盘文件的类型和格式
l_buffer raw(32767);--定义缓冲区大小
l_amount binary_integer := 3276; --每次位移个数
l_pos int :=1;--开始位置
l_blob blob;--临时数据存放
l_blob_len int;--总长度
begin
select content into l_blob from test001; --将数据库中的数据,存放在blob变量中
--获取blob文件的长度
l_blob_len := dbms_lob.getlength(l_blob);
--准备写入磁盘文件
l_file := utl_file.fopen('TEST_DIR','HHAHAHAHAHAHAHAHAAHA.JPG','wb');
--写入数据
while l_pos<l_blob_len loop
dbms_lob.read(l_blob,l_amount,l_pos,l_buffer);
utl_file.put_raw(l_file,l_buffer,true);
l_pos := l_pos + l_amount;
end loop;
utl_file.fclose(l_file);
dbms_output.put_line('恭喜,恭喜。。。。文件写成功!');
end;
/*
文本大对象的写入和读取(clob)。
*/
--写入文本文件第一种方式
declare
tempimg clob;--定义临时变量存放数据
tempdir bfile := bfilename('TEST_DIR','DIV3.html');--非常重要:所有数据都是大写存放的
amount int:=dbms_lob.getlength(tempdir);
src_offset int:=1;
dest_offset int:=1;
csid int:=0;
lc int:=0;
warning int;
begin
insert into test002 values ('第四个文本文件',empty_clob()) returning content into tempimg;
--使用内置的包,给tempimg写入数据
dbms_lob.fileopen(tempdir);--打开指定文件
dbms_lob.loadclobfromfile(tempimg,tempdir,amount,dest_offset,src_offset,csid,lc,warning);
dbms_lob.fileclose(tempdir);--关闭文件
dbms_output.put_line('恭喜你,终于成功了!!!');
commit;
end ;
--写入文本文件第二种方式(通过异常判断文件结束的)
declare
filecontent clob;
input_file utl_file.file_type;
buffer varchar2(2000);
l_pos int := 1;
amount int;
begin
insert into test002 values ('第二个文本数据',empty_clob()) returning content into filecontent;
--打开磁盘文件
input_file := utl_file.fopen('TEST_DIR','DIV3.html','r');
loop
utl_file.get_line(input_file,buffer);
--获取每次读取的长度
amount:=length(buffer);--每次写入的字符长度
exit when amount<=0;
dbms_lob.write(filecontent,amount,l_pos,buffer);
l_pos:=l_pos+amount;
end loop;
utl_file.fclose(input_file);
dbms_output.put_line('文件写入完毕!');
exception
when no_data_found then
dbms_output.put_line('数据已经读取完毕了!');
utl_file.fclose(input_file);
end;
select * from test002
--读取表中的数据,到文件
declare
src clob;
outfile utl_file.file_type;
length integer;
buffer varchar2(8000);
begin
select content into src from test002 where fname='第四个文本文件';
length := dbms_lob.getlength(src);
dbms_lob.read(src,length,1,buffer);
--打开磁盘文件
outfile := utl_file.fopen('TEST_DIR','hahahahhahahahah.html','w',8000);
utl_file.put(outfile,buffer);--写入数据
utl_file.fclose(outfile); --关闭指针
dbms_output.put_line('文件已经写入完毕!');
end;
最新文章
- java的system.arraycopy()方法
- js sort() 排序的问题
- 转:电子取证中AVI文件的文件雕复
- NIO与普通IO文件读写性能对比
- win10 下安装、配置、启动mysql
- leetcode第36题--Sudoku Solver
- Vijos1055 奶牛浴场(极大化思想求最大子矩形)
- 28.Implement strStr()【leetcod】
- JavaScript操作符(布尔操作符、乘性操作符和加性操作符)
- Mybatis:缓存,动态SQL,注解SQL以及动态标签使用
- Linux下修改Jenkins默认端口
- lex yacc flex bison
- Redis source code analysis
- 如何使ul中li元素横向排列且不换行
- onclick传参
- jvm高级特性(5)(1)(原子性,可见性,有序性,volatile,概述)
- 8 种提升 ASP.NET Web API 性能的方法 (转)
- Spring入门(三)— AOP注解、jdbc模板、事务
- java 父类的引用调用自己的属性 但是调用的方法必须是重写过的父类的方法 因为编译时候把他当作父类 运行时候才是他自己 所以必须重写父类得方法
- BZOJ3747 POI2015 Kinoman 【线段树】*
热门文章
- PHP 类属性
- [转][osg]探索未知种族之osg类生物【目录】
- flutter Radio单选框
- 如何解决Access操作或事件已被禁用模式阻止
- 我的一个PLSQL函数 先查询再插入数据库的函数 动态SQL拼接查询条件、通用游标、记录定义(封装部分查询字段并赋值给游标)、insert select 序列、常量【我】
- QThread: Destroyed while thread is still running
- How to Plan and Configure YARN and MapReduce 2 in HDP 2.0
- Django安全配置(settings.py)详解
- CSS网站框架及样式命名规范
- consui(二)集群配置