Oracle 读写文件 bfilename [实例]

SELECT directory_name, 
    grantee, 
    privilege  FROM user_tab_privs t, 
    all_directories d  WHERE t.table_name(+)=d.directory_name  ORDER BY 1,2,3;

让我们看一个简单的测试:
SQL> create or replace directory UTL_FILE_DIR as
‘/opt/oracle/utl_file’;Directory created.
SQL> declare  
 2    fhandle utl_file.file_type;
 3  begin  
ylg娱乐官网, 4    fhandle := utl_file.fopen(‘UTL_FILE_DIR’, ‘example.txt’, ‘w’);
 
 5    utl_file.put_line(fhandle , ‘eygle test write one’);  
 6    utl_file.put_line(fhandle , ‘eygle test write two’);  
 7    utl_file.fclose(fhandle);  
 8  end;  
 9  /
 PL/SQL procedure successfully completed.
 SQL> !
 [oracle@jumper 9.2.0]$ more /opt/oracle/utl_file/example.txt eygle
test write oneeygle test write two[oracle@jumper 9.2.0]$
 

查询DIRECTORY的读写权限,SQL如下:

此时用户eygle就拥有了对该目录的读写权限。

语法如下:

本案例具体创建如下:
create or replace directory exp_dir as ‘/tmp’;
 

 

可以查询dba_directories查看所有directory.
SQL> select * from dba_directories;
      OWNER                          DIRECTORY_NAME                
DIRECTORY_PATH

select * from all_directories;

目录创建以后,就可以把读写权限授予特定用户,具体语法如下:
GRANT READ[,WRITE] ON DIRECTORY directory TO username;

可以通过如下SQL查看已经创建的DIRECTORY

Oracle 读写文件 bfilename [实例]
Create
directory让我们可以在Oracle数据库中灵活的对文件进行读写操作,极大的提高了Oracle的易用性和可扩展性。
其语法为:
CREATE [OR REPLACE] DIRECTORY directory AS ‘pathname’;

创建后如果要对该路径下文件进行读写,必须授权。

      —————————— ——————————

      SYS                            UTL_FILE_DIR                  
/opt/oracle/utl_fileSYS                            
                                      BDUMP_DIR                     
/opt/oracle/admin/conner/bdumpSYS                                                        
EXP_DIR                        /opt/oracle/utl_file
 

可以使用drop directory删除这些路径.
SQL> drop directory exp_dir;
Directory dropped
SQL> select * from dba_directories;
OWNER                          DIRECTORY_NAME                
DIRECTORY_PATH



SYS                            UTL_FILE_DIR                  
/opt/oracle/utl_fileSYS                            
                               BDUMP_DIR                     
/opt/oracle/admin/conner/bdump
 

create or replace directory USER_DIR as ‘E:PLSQL310’;

DECLARE
   v_content VARCHAR2(1800);
   v_bfile BFILE;
   amount INT;
   offset INT :=1;
   
BEGIN
v_bfile := bfilename(‘USER_DIR’,’test.TXT’); — 注意这里的 User_dir
对应上面已经创建好啦的目录
amount :=DBMS_LOB.getlength(v_bfile);
DBMS_LOB.OPEN(v_bfile);
DBMS_LOB.READ(v_bfile,amount,offset,v_content);
DBMS_LOB.close(v_bfile);
DBMS_OUTPUT.PUT_LINE(v_content);
END;

GRANT READ,WRITE ON DIRECTORY UTL_FILE_DIR TO scott;

类似的我们可以通过utl_file来读取文件:
SQL> declare  
 2    fhandle   utl_file.file_type;  
 3    fp_buffer varchar2(4000);  
 4  begin  
 5    fhandle := utl_file.fopen (‘UTL_FILE_DIR’,’example.txt’, ‘R’);
 
 6    
 7    utl_file.get_line (fhandle , fp_buffer );  
 8    dbms_output.put_line(fp_buffer );  
 9    utl_file.get_line (fhandle , fp_buffer );
 10    dbms_output.put_line(fp_buffer );
 11    utl_file.fclose(fhandle);
 12  end;
 13  /
 eygle test write one
 eygle test write two
 PL/SQL procedure successfully completed.
 

发表评论

电子邮件地址不会被公开。 必填项已用*标注