自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Oracle存儲(chǔ)過(guò)程讀寫(xiě)文件

數(shù)據(jù)庫(kù) Oracle
有時(shí)候我們需要在文件與數(shù)據(jù)庫(kù)表之間利用程序來(lái)實(shí)現(xiàn)兩者的交互,下文介紹的就是Oracle存儲(chǔ)過(guò)程讀寫(xiě)文件的實(shí)現(xiàn)方法,供您參考。

Oracle存儲(chǔ)過(guò)程讀寫(xiě)文件是實(shí)現(xiàn)文件與數(shù)據(jù)庫(kù)表之間交互的重要手段,下面就為您詳細(xì)介紹Oracle存儲(chǔ)過(guò)程讀寫(xiě)文件方面的知識(shí),希望對(duì)您能有所幫助。

有時(shí)候我們需要在文件與數(shù)據(jù)庫(kù)表之間利用程序來(lái)實(shí)現(xiàn)兩者的交互,這里可以利用UTL_FILE包實(shí)現(xiàn)對(duì)文件的I/O操作.下面就分別介紹文件寫(xiě)表以及表數(shù)據(jù)寫(xiě)文件.

[1]表信息導(dǎo)出到文件

在SUSE上建議一個(gè)文件夾/home/zxin10/file,然后對(duì)其chmod g+w file進(jìn)行授權(quán)(否則無(wú)法導(dǎo)出到文件),再對(duì)您指定的路徑(/home/zxin10/file)向Oracle的系統(tǒng)表sys.dir$進(jìn)行注冊(cè)(否則也是無(wú)法成功將信息導(dǎo)出到文件),操作完后可以查詢(xún)sys.dir$可以看到表中的OS_PATH中有您指定的路徑位置.

注冊(cè)方式:執(zhí)行SQL語(yǔ)句create or replace directory BBB as '/home/zxin10/file'; 即可

存儲(chǔ)過(guò)程如下:(寫(xiě)文件時(shí),文件名可以不用先創(chuàng)建,程序中會(huì)自動(dòng)創(chuàng)建指定文件)

  1. CREATE OR REPLACE PROCEDURE V3_SUB_FETCH_TEST_2  
  2. (  
  3.    V_TEMP VARCHAR2,  
  4.    --1為成功,0為失敗  
  5.    v_retvalue   OUT NUMBER   
  6.  )  
  7. AS  
  8.   --游標(biāo)定義  
  9.   type ref_cursor_type is REF CURSOR;  
  10.   cursor_select   ref_cursor_type;  
  11.   select_cname    varchar2(1000);  
  12.     
  13.   v_file_handle   utl_file.file_type;  
  14.     
  15.   v_sql varchar2(1000);  
  16.   v_filepath Varchar2(500);  
  17.   v_filename Varchar2(500);  
  18.   --緩沖區(qū)  
  19.   v_results Varchar2(500);  
  20.     
  21.   v_pid varchar2(1000);  
  22.   v_cpcnshortname Varchar2(500);  
  23.    
  24.   begin  
  25.       v_filepath :V_TEMP;  
  26.       if v_filepath is null then  
  27.          v_filepath :'/home/zxin10/file3';  
  28.       end if;  
  29.       v_filename:='free_'|| substr(to_char(sysdate,'YYYYMMDDHH24MI'),1,10) ||'.all' ;  
  30.       --游標(biāo)開(kāi)始  
  31.       select_cname:='select cpid,cpcnshortname from zxdbm_ismp.scp_basic';   
  32.       --打開(kāi)一個(gè)文件句柄 ,同時(shí)fopen的***個(gè)參數(shù)必須是大寫(xiě)     
  33.       v_file_handle:=utl_file.fopen('BBB',v_filename,'A');  
  34.       Open cursor_select For select_cname;     
  35.       Fetch  cursor_select into v_pid,v_cpcnshortname;  
  36.       While  cursor_select%Found     
  37.       Loop  
  38.       v_results :v_pid||'|'||v_cpcnshortname;  
  39.       --將v_results寫(xiě)入文件  
  40.       utl_file.put_line(v_file_handle,v_results);     
  41.       Fetch  cursor_select into v_pid,v_cpcnshortname;        
  42.       End Loop;  
  43.         
  44.       Close cursor_select;--關(guān)閉游標(biāo)  
  45.       utl_file.fClose(v_file_handle);--關(guān)閉句柄  
  46.       v_retvalue :=1;  
  47.   exception when others then  
  48.          v_retvalue :=0;   
  49.   end V3_SUB_FETCH_TEST_2; 

[2]將文件信息導(dǎo)入到表中

和上面一樣,先對(duì)指定文件路徑進(jìn)行chmod,然后想Oracle的sys.dir$進(jìn)行路徑注冊(cè).

文件zte.apsuic位于/home/zxin10/file下,其數(shù)據(jù)格式:
1|22|cheng
2|33|zhou
3|44|heng
4|55|yaya

表LOADDATA腳本:

  1. -- Create table  
  2. create table LOADDATA  
  3. (  
  4.   ID   VARCHAR2(50),  
  5.   AGE  VARCHAR2(50),  
  6.   NAME VARCHAR2(50)  
  7. )  
  8.     / 

程序如下:(讀取文件時(shí),指定文件名一定要預(yù)先存在,否則程序會(huì)失敗)

  1. create or replace directory BBB as '/home/zxin10/file';  
  2. /  
  3. --作用法是將特定的文件路徑信息想Oracle注冊(cè)(注冊(cè)信息存放在sys.dir$表中)  
  4.  
  5. CREATE OR REPLACE PROCEDURE V3_SUB_FETCH_TEST_3  
  6. (  
  7.    --文件中的信息導(dǎo)入表中  
  8.      V_TEMP VARCHAR2,  
  9.      v_retvalue   OUT NUMBER --1 成功 ,0失敗  
  10. AS   
  11.   v_file_handle   utl_file.file_type;    
  12.   v_sql varchar2(1000);  
  13.   v_filepath Varchar2(500);  
  14.   v_filename Varchar2(500);    
  15.   --文件到表字段的映射  
  16.   v_id varchar2(1000);  
  17.   v_age varchar2(1000);  
  18.   v_name varchar2(1000);  
  19.   --緩沖區(qū)  
  20.   v_str varchar2(1000);  
  21.   --列指針  
  22.   v_i number;  
  23.   --字符串定位解析指針  
  24.   v_sposition1 number;  
  25.   v_sposition2 number;  
  26.   begin  
  27.       v_filepath :V_TEMP;  
  28.       if v_filepath is null then  
  29.          v_filepath :'/home/zxin10/file';  
  30.       end if;  
  31.       v_filename:='zte.apsuic';  
  32.       --v_sql:= 'create or replace directory CCC as '''|| v_filepath || '''';  
  33.       --execute immediate v_sql;   
  34.         
  35.       v_file_handle:=utl_file.fopen('CCC',v_filename,'r');         
  36.       Loop  
  37.              --將文件信息讀取到緩沖區(qū)v_str中,每次讀取一行  
  38.              utl_file.get_line(v_file_handle,v_str);  
  39.              --dbms_output.put_line(v_str);   
  40.              --針對(duì)每行的列數(shù)  
  41.              v_i :1;  
  42.              --針對(duì)字符串每次的移動(dòng)指針  
  43.              v_sposition1 :1;  
  44.              --文件中每行信息3列,循環(huán)3次  
  45.              FOR I IN 1..3 loop                 
  46.              --當(dāng)instr(v_str, '|', 6)其中v_str為1|22|wuzhuocheng ,它返回0  
  47.              v_sposition2 :instr(v_str, '|', v_sposition1);  
  48.              --字符串解析正常情況  
  49.              if v_sposition2 <> 0 then  
  50.                if v_i=1     then  
  51.                   v_id :substr(v_str, v_sposition1, v_sposition2 - v_sposition1);      --***列                 
  52.                elsif  v_i=2 then  
  53.                     v_age :substr(v_str, v_sposition1, v_sposition2 - v_sposition1); --第二列  
  54.                elsif v_i=3  then  
  55.                   v_name :substr(v_str, v_sposition1, v_sposition2 - v_sposition1);    --第三列   
  56.                else  
  57.                   return;  
  58.                end if;                              
  59.              --字符串解析異常情況  
  60.              else   
  61.                if v_i=1    then  
  62.                 v_id :substr(v_str, v_sposition1);      --***列  
  63.                elsif v_i=2 then  
  64.                   v_age :substr(v_str, v_sposition1); --第二列  
  65.                elsif v_i=3 then  
  66.                 v_name :substr(v_str, v_sposition1);    --第三列   
  67.                else  
  68.                 return;  
  69.                end if;    
  70.              end if;     
  71.              v_sposition1 :v_sposition2 + 1;  
  72.              v_i :v_i+1;  
  73.              end loop;   
  74.              --每列循環(huán)完后將信息insert into表中  
  75.              insert into zxdbm_ismp.loaddata values(v_id,v_age,v_name);                    
  76.       End Loop;  
  77.       --關(guān)閉句柄  
  78.       utl_file.fClose(v_file_handle);  
  79.       v_retvalue :=1;  
  80.   exception when others then  
  81.          v_retvalue :=0;   
  82.   end V3_SUB_FETCH_TEST_3;  

 

 

 

 

【編輯推薦】

Oracle讀寫(xiě)文件bfilename的實(shí)例

Oracle XE自帶數(shù)據(jù)庫(kù)創(chuàng)建的過(guò)程

Oracle物化視圖創(chuàng)建全過(guò)程

ORACLE創(chuàng)建實(shí)例的過(guò)程

oracle時(shí)間加減的語(yǔ)句寫(xiě)法

責(zé)任編輯:段燃 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-04-16 10:11:20

Oracle存儲(chǔ)過(guò)程

2011-04-15 10:56:22

2011-04-13 09:45:15

Oracle存儲(chǔ)

2009-03-25 10:48:08

存儲(chǔ)銀行Oracle

2010-04-07 13:12:25

Oracle存儲(chǔ)過(guò)程

2009-11-05 18:07:33

Oracle導(dǎo)出sql

2010-04-07 12:08:28

Oracle存儲(chǔ)過(guò)程

2009-05-13 10:29:01

存儲(chǔ)過(guò)程OracleJava

2009-12-21 09:39:50

Oracle 存儲(chǔ)過(guò)程

2010-11-12 12:01:08

Oracle存儲(chǔ)過(guò)程

2010-11-19 10:57:43

Oracle讀寫(xiě)文件

2010-04-15 17:31:10

Oracle存儲(chǔ)過(guò)程

2010-04-15 18:06:08

Oracle存儲(chǔ)過(guò)程

2010-10-29 16:06:55

Oracle存儲(chǔ)過(guò)程

2018-10-12 11:26:13

oracle存儲(chǔ)語(yǔ)法

2010-04-08 16:41:29

Oracle存儲(chǔ)過(guò)程

2010-11-16 14:30:32

Oracle存儲(chǔ)過(guò)程

2018-08-28 11:40:47

存儲(chǔ)過(guò)程語(yǔ)法

2010-11-19 11:22:25

oracle對(duì)系統(tǒng)文件

2010-10-26 14:50:11

oracle存儲(chǔ)過(guò)程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)