oracle利用utl file包來讀寫檔案

2021-09-30 03:59:14 字數 2163 閱讀 9598

oracle利用使用utl_file包

create or replace procedure loadfiledata(p_path varchar2,p_filename varchar2) is

v_filehandle utl_file.file_type; --定義乙個檔案控制代碼

v_text varchar2(100); --存放文字

v_name test.name%type;

v_id test.autoid%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_id:=substr(v_text,1,v_firstlocation-1);

v_name:=substr(v_text,v_firstlocation+1);

/*插入資料庫操作*/

insert into test

values (v_id,v_name);

commit;

end loop;

<;>;

null;

end loadfiledata;

***************=我建立表*****

3. 測試環境

首先要建立乙個目標表,它用來儲存檔案中的資料:

create table test (

autoid varchar2(10);

name varchar2(20));

*************************=

declare

v_path varchar2(200);

v_filename varchar2(200);

begin

v_path:='f: ';

v_filename:='位址資訊.txt';

loadfiledata(v_path,v_filename);

end; /

/* 由於oracle資料庫對包建立的目錄有乙個安全管理的問題,所以並不是所有的檔案目錄能夠被utl_file包所訪問,

要更新這種目錄設定,就得到init.ora裡將utl_file_dir域設定為*,這樣utl_file包就可以對所有的目錄檔案進行訪問了,utl_file_dir我設定有正確*/

*****出現錯誤呀提示!*****=

ora-06510: pl/sql: 無法處理的使用者自定義異常事件

ora-06512: 在"sys.utl_file", line 98

ora-06512: 在"sys.utl_file", line 157

ora-06512: 在"system.loadfiledata2", line 15

ora-06512: 在line 1

***************===

***************====

declare

file_handle utl_file.file_type;

begin

file_handle:=utl_file.fopen('c:temp','sss.txt','a');

utl_file.put_line(file_handle,'寫入的資訊');

utl_file.fclose(file_handle);

end;

*****===

在init.ora中加入utl_file_dir = c:temp

重起後就行了

oracle的檔案(UTL FILE)操作

oracle提供了乙個能否對作業系統操作的工具包utl file 想要oracle對檔案進行操作就要先建立乙個directory來指向作業系統目錄下的具體某個目錄 create directory report dir as home oracle chenlong report dir 為建立di...

利用UTL FILE包實現檔案I O操作

檔案i o對於資料庫的開發來說顯得很重要,比如如果資料庫中的一部分資料來自於磁碟檔案,那麼就需要使用i o介面把資料匯入到資料庫中來。在pl sql中沒有直接的i o介面,一般在除錯程式時可以使用oracle自帶的dbms output包的put line函式 即向螢幕進行i o操作 即可,但是對於...

oracle中utl file包讀寫檔案操作學習

oracle中utl file包讀寫檔案操作學習 在oracle中utl file包提供了一些操作文字檔案的函式和過程,學習了一下他的基本操作 www.2cto.com 1.建立directory,並給使用者授權 建立directory create or replace directory tes...