第一步:以管理員用戶(hù)登陸
如:conn sys/password@sid as sysdba
第二步:設置可操作目錄
需要指定utl_file包可以操作的目錄。在oracle 10g以前,可以用以下方法:
1、alter system set utl_file_dir='e:\utl' scope=spfile;
2、在init.ora文件中,配置如下:
UTL_FILE=E:\utl或者UTL_FILE_DIR=E:\utl
在oracle 10g中建議用以下方法配置:CREATE DIRECTORY utl AS 'E:\utl';
參見(jiàn)oracle online:
In the past, accessible directories for the UTL_FILE functions were specified in the initialization file using the UTL_FILE_DIR parameter. However, UTL_FILE_DIR access is not recommended. It is recommended that you use the CREATE DIRECTORY feature, which replaces UTL_FILE_DIR. Directory objects offer more flexibility and granular control to the UTL_FILE application administrator, can be maintained dynamically (that is, without shutting down the database), and are consistent with other Oracle tools. CREATE DIRECTORY privilege is granted only to SYS and SYSTEM by default.
第三步:授權給指定用戶(hù),以便執行utl_file
GRANT EXECUTE ON utl_file TO scott;
第四步:conn scott/tiger
就可以正常使用utl_file了。
摘要:本文主要討論如何利用Oracle的UTL_FILE包來(lái)實(shí)現對磁盤(pán)文件的I/O操作。
文件I/O對于數據庫的開(kāi)發(fā)來(lái)說(shuō)顯得很重要,比如如果數據庫中的一部分數據來(lái)自于磁盤(pán)文件,
那么就需要使用I/O接口把數據導入到數據庫中來(lái)。在 PL/SQL中沒(méi)有直接的I/O接口,
一般在調試程序時(shí)可以使用Oracle自帶的DBMS_OUTPUT包的put_line函數(即向屏幕進(jìn)行I/O 操作)即可,
但是對于磁盤(pán)文件的I/O操作它就無(wú)能為力了。其實(shí)Oracle同樣也提供了可以進(jìn)行文件I/O的實(shí)用包-----UTL_FILE包,
利用這個(gè)實(shí)用包提供的函數來(lái)實(shí)現對磁盤(pán)的I/O操作。
1. 準備工作
由于Oracle數據庫對包創(chuàng )建的目錄有一個(gè)安全管理的問(wèn)題,所以并不是所有的文件目錄能夠被UTL_FILE包所訪(fǎng)問(wèn),
要更新這種目錄設置,就得到init.ora里將UTL_FILE_DIR域設置為*,這樣UTL_FILE包就可以對所有的目錄文件進(jìn)行訪(fǎng)問(wèn)了。
2. 文件I/O的實(shí)施
UTL_FILE包提供了很多實(shí)用的函數來(lái)進(jìn)行I/O操作,主要有以下幾個(gè)函數:
fopen
打開(kāi)指定的目錄路徑的文件。
get_line
獲取指定文件的一行的文本。
put_line
向指定的文件寫(xiě)入一行文本。
fclose
關(guān)閉指定的文件。
下面利用這些函數,實(shí)現從文件取數據,然后將數據寫(xiě)入到相應的數據庫中。
create or replace procedure loadfiledata(p_path varchar2,p_filename varchar2) as
v_filehandle utl_file.file_type; --定義一個(gè)文件句柄
v_text varchar2(100); --存放文本
v_name test_loadfile.name%type;
v_addr_jd test_loadfile.addr_jd%type;
v_region test_loadfile.region%type;
v_firstlocation number;
v_secondlocation number;
v_totalinserted number;
begin
if (p_path is null or p_filename is null) then
goto to_end;
end if;
v_totalinserted:=0;
/*open specified file*/
v_filehandle:=utl_file.fopen(p_path,p_filename,'r');
loop
begin
utl_file.get_line(v_filehandle,v_text);
exception
when no_data_found then
exit;
end
v_firstlocation:=instr(v_text,',',1,1);
v_secondlocation:=instr(v_text,',',1,2);
v_name:=substr(v_text,1,v_firstlocation-1);
v_addr_jd:=substr(v_text,v_firstlocation+1,v_secondlocation-v_firstlocation-1);
v_region:=substr(v_text,v_secondlocation+1);
/*插入數據庫操作*/
insert into test_loadfile
values (v_name,v_addr_jd,v_region);
commit;
end loop;
<<to_end>>
null;
end loadfiledata;
可以不用在init.ora中改的
只要用管理員的權限登陸,執行:
create directory UTL_FILE_TEST as '\*'
應該就可以了,不用新啟動(dòng)oracle的
不過(guò)其他用戶(hù)要使用此目錄要授權的
/
grant create any directory to scott;
grant create any library to scott;
create or replace directory utllobdir as 'C:\ep';
在initsid.ora文件中,加入或修改
設置utl_file_dir的要點(diǎn):
1。 utl_file_dir=* 這表示你能操作任何目錄,盡量不要用
2。 utl_file_dir=d:\ 這表示你能操作d:\目錄下的文件,但你不能操作d:\目錄下的子目錄
3。注意在設置
utl_file_dir=路徑時(shí),如果路徑是長(cháng)路徑名,例如c:\my temp目錄,則你必須加上'',例如:
utl_file_dir='c:\my temp'
4。utl_file_dir可以是多個(gè)路徑
utl_file_dir=c:\,d:\,d:\temp,'c:\my temp'
5。設置完必須重新啟動(dòng)數據庫