oracle利用utl_file包來讀寫檔案
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
重起後就行了[@more@]
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
重起後就行了[@more@]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/101162/viewspace-1009209/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 檔案排版(文字檔案讀寫)
- Golang 讀、寫檔案Golang
- Python 讀寫檔案Python
- Python——檔案讀寫Python
- keras讀寫檔案Keras
- 「Python」:檔案讀寫Python
- 檔案的讀寫
- python讀寫excel檔案PythonExcel
- C++讀寫檔案C++
- 普通檔案的讀寫
- python檔案讀寫操作Python
- C++檔案讀寫C++
- VBA建立文字檔案、讀寫文字檔案
- 讀取檔案流並寫入檔案流
- Python:讀寫檔案(I/O) | 組織檔案Python
- nodejs xmlreader 讀寫xml檔案NodeJSXML
- Python中的檔案讀寫Python
- C++檔案讀寫操作C++
- Golang對檔案讀寫操作Golang
- Java 字元流檔案讀寫Java字元
- C語言-檔案讀寫C語言
- Perl讀寫檔案&字串操作字串
- Python 檔案讀寫(Python IO)Python
- c風格讀寫檔案
- java 讀寫 ini 配置檔案Java
- C/C++ 檔案讀寫C++
- C++讀寫檔案操作C++
- C#讀取文字檔案和寫文字檔案C#
- (資料科學學習手札63)利用pandas讀寫HDF5檔案資料科學
- Python檔案讀寫、StringIO和BytesIOPython
- Perl IO:隨機讀寫檔案隨機
- 使用POI讀寫word docx檔案
- 使用C#讀寫ini檔案C#
- 使用C#讀寫xml檔案C#XML
- python config配置檔案的讀寫Python
- Python之檔案讀寫小練Python
- C#讀寫檔案總結C#
- numpy陣列之讀寫檔案陣列
- python學習之讀寫檔案Python