Oracle读写文件操作相信大家都不陌生,下面就为您介绍Oracle读写文件bfilename的实例,希望对您学习Oracle读写文件方面能有所帮助。
Create directory让我们可以在Oracle数据库中灵活的对文件进行读写操作,极大的提高了Oracle的易用性和可扩展性。 其语法为: CREATE [OR REPLACE] DIRECTORY directory AS ‘pathname’;
本案例具体创建如下: create or replace directory exp_dir as ‘/tmp’;
目录创建以后,就可以把读写权限授予特定用户,具体语法如下: GRANT READ[,WRITE] ON DIRECTORY directory TO username;
例如: grant read, write on directory exp_dir to eygle;
此时用户eygle就拥有了对该目录的读写权限。
让我们看一个简单的测试:
SQL>createorreplacedirectoryUTL_FILE_DIRas'/opt/oracle/utl_file'; Directorycreated.SQL>declare fhandleutl_file.file_type; begin fhandle:=utl_file.fopen('UTL_FILE_DIR','example.txt','w'); utl_file.put_line(fhandle,'eygletestwriteone'); utl_file.put_line(fhandle,'eygletestwritetwo'); utl_file.fclose(fhandle); end; / PL/SQLproceduresuccessfullycompleted. SQL>! [oracle@jumper9.2.0]$more/opt/oracle/utl_file/example.txt eygletestwriteone eygletestwritetwo [oracle@jumper9.2.0]$
类似的我们可以通过utl_file来读取文件:
SQL>declare fhandleutl_file.file_type; fp_buffervarchar2(4000); begin fhandle:=utl_file.fopen('UTL_FILE_DIR','example.txt','R'); utl_file.get_line(fhandle,fp_buffer); dbms_output.put_line(fp_buffer); utl_file.get_line(fhandle,fp_buffer); dbms_output.put_line(fp_buffer); utl_file.fclose(fhandle); end; / eygletestwriteone eygletestwritetwo PL/SQLproceduresuccessfullycompleted.
可以查询dba_directories查看所有directory.
SQL>select*fromdba_directories; OWNERDIRECTORY_NAMEDIRECTORY_PATH------------------------------------------------------------------------------------------ SYSUTL_FILE_DIR/opt/oracle/utl_file SYSBDUMP_DIR/opt/oracle/admin/conner/bdump SYSEXP_DIR/opt/oracle/utl_file
可以使用drop directory删除这些路径.
SQL>dropdirectoryexp_dir;DirectorydroppedSQL>select*fromdba_directories;OWNERDIRECTORY_NAMEDIRECTORY_PATH------------------------------------------------------------------------------------------SYSUTL_FILE_DIR/opt/oracle/utl_fileSYSBDUMP_DIR/opt/oracle/admin/conner/bdump createorreplacedirectoryUSER_DIRas'E:\PLSQL\310\'; DECLARE v_contentVARCHAR2(1800); v_bfileBFILE; amountINT; offsetINT:=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;
相关文章
标签:Oracle