الانتقال إلى المحتوى

كيفية قراءة بيانات من ملف نصي و ادخالها الى قاعدة


sahar2

Recommended Posts

عرف الملف في البداية
in_file Text_IO.File_Type;
عرف متغير لتقراء عليه
linebuf VARCHAR2(80);


افتح الملف
in_file := Text_IO.Fopen('موقع الملف', 'r/w');
مثال
in_file := Text_IO.Fopen('C:\SALARY_DISK.txt', 'r');


اقراء من الملف
Text_IO.Get_Line(in_file,linebuf);



لما تنتهي قفل الملف
Text_IO.Fclose(in_file);


استخدمت هالفنكشن للكتابة على ملف فهل تنفع بالقراءه يفترض انها تنفع لكن يبي لك تجرب

رابط هذا التعليق
شارك

الاخ الكريم
هذا شرح مفصل عن عملية استخدام الملف وكيفية التعامل معها


USING THE TEXT_IO PACKAGE






Document ID: 11165515.61
Title: Using the TEXT_IO Package
Creation Date: 30 June 1996
Last Revision Date: 27 August 1996
Revision Number: 1
Product: Developer/2000 Tools
Product Version: V4.5, V2.5
Platform: GENERIC
Information Type: INQUIRY
Impact: MEDIUM
Abstract: This document describes the functionality of the TEXT_IO
package and explains how to use it to access the file
system effectively.
Keywords: TEXT_IO;FILE;IO;PACKAGE

_______________________________________________________________________________

Using the TEXT_IO Package
=========================

Oracle Procedure Builder, "an integrated, interactive environment for
PL/SQL", is shipped with several built-in packages which, if used
properly, can greatly boost development productivity. These built-in
packages are not installed as extensions to the STANDARD package;
hence, when you reference a construct from any of these packages, you
must prefix the package name to the construct.

TEXT_IO provides constructs that allow programmers to read information
from and write information to a file system. The procedures and
functions in TEXT_IO fall into the following three categories:

Category 1: File Operations
---------------------------
FILE_TYPE - Declares a file type variable as a handle to the file being
processed.
Syntax: TEXT_IO.FILE_TYPE;

FOPEN - Opens a file and returns a handle to the specified file of
type FILE_TYPE.
Syntax: TEXT_IO.FOPEN(file_specs VARCHAR2, file_mode VARCHAR2);
The file mode can be R(read), W(write) or A(append).

FCLOSE - Closes an open file.
Syntax: TEXT_IO.FCLOSE(file_specs VARCHAR2);

IS_OPEN - Checks if the specified file is currently open and returns a
boolean.
Syntax: TEXT_IO.IS_OPEN(file_type FILE_TYPE);


Category 2: Output Operations
-----------------------------
PUT - Concatenates the supplied data to the current line of an open file.
Syntax: TEXT_IO.PUT(file FILE_TYPE, data DATA_TYPE)
Valid data types are VARCHAR2, DATE, NUMBER, PLS_INTEGER.
All data except VARCHAR2 is converted to a character string.

PUTF - Formats and writes a character string to an open file,
doing a C-like substitution.
Syntax: TEXT_IO.PUTF(file FILE_TYPE, format VARCHAR2,
arg1[,...,arg5] VARCHAR2);
Format is an optional parameter. If it is omitted, only
one argument value can be passed.
You can embed up to 5 "%s" patterns within the format string,
which can be replaced by the corresponding strings
(arg1 ... arg5) at execution.

PUT_LINE - Concatenates the given argument string to the file
followed by a newline character.
Syntax: TEXT_IO.PUT_LINE(file FILE_TYPE, data VARCHAR2);

NEW_LINE - Concatenates n number of newline characters to the current line,
where n is the second argument with a default value of 1.
Syntax: TEXT_IO.NEW_LINE(file FILE_TYPE, n PLS_INTEGER := 1);

Note: If the file name is not specified each of the above cases, the
output is directed to the Interpreter.


Category 3: Input Given Operations
----------------------------------
GET_LINE - Reads a line from an open file into the parameter.
Syntax: TEXT_IO.GET_LINE(file FILE_TYPE, data_item OUT VARCHAR2);

Note: After a GET_LINE call, the file pointer moves to the next
line and subsequent lines are read in after each call. If the
line to be read exceeds the size of data_item, the VALUE_ERROR
exception is raised. If no more characters can be read from the
file, the NO_DATA_FOUND exception is raised. You cannot move this
pointer back to the previous line though this can be simulated
using your own buffer scheme.

If a file name is provided for any of the above routines, this file
must be accessible to Oracle Forms. In addition, you must provide the
complete path. If no path is given, Forms assumes the file is in the
current working directory and does not search for the file in the file
system. The accessibility is determined as follows:

On Windows, a TEXT_IO package function call only accesses local
drives. It does not recognize any Universal Naming Conventions like
\\machine_name\drive_letter. To access files on remote machines using
the TEXT_IO package, map a letter to the remote drive through the File
Manager.

On UNIX, the absolute path must be given for the location of the file.
Forms does not recognize relative paths provided for the file. This is also
the case for VMS and other platforms. When you do not specify a path, Forms
assumes the file is in the current working directory.

For more information, refer to the TEXT_IO Package section in the
"Oracle Procedure Builder Packages" chapter of the Oracle
Developer/2000 Procedure Builder Developer's Guide Manual.


Sample Form
-----------
The following sample form explains some of the concepts and the
syntax. The form reads in lines from a specified text file one at a
time and displays them in a text item. It enables the user to parse
the file by getting the next or the previous line. In addition, the
user can write any line currently displayed to a specified output
file.

Create a single non-base table block. Put the following items and
buttons on it. Create the appropriate boilerplate text for each item.
All items go on the same canvas. No database connection is required
for this application.

Create the following text items:
1. Name INPUT_FILE_SPECS
Item Type Text Item
Data Type Char
Maximum Length 30

2. Name LINE_READ
Item Type Text Item
Data Type Char
Maximum Length 300

3. Name OUTPUT_FILE_SPECS
Item Type Text Item
Data Type Char
Maximum Length 30

4. Name LINE_NO
Item Type Text Item
Data Type Number
Maximum Length 30

Create the following buttons and associated triggers:
1. Name OPEN_AND_READ_FROM_FILE
Label Echo file using TEXT_IO package

Associated Button Trigger:
Name WHEN-BUTTON-PRESSED
Trigger Text:
IF :input_file_specs IS NOT NULL THEN
:line_no := 1;
ECHO_TEXT_IO;
ELSE
MESSAGE('You must enter a valid input file name
with a complete path');
END IF;


2. Name NEXT
Label NEXT LINE

Associated Button Trigger:
Name WHEN-BUTTON-PRESSED
Trigger Text:
:line_no := :line_no + 1;
ECHO_TEXT_IO;


3. Name PREVIOUS
Label PREVIOUS LINE

Associated Button Trigger:
Name WHEN-BUTTON-PRESSED
Trigger Text:
IF :line_no = 1 THEN
MESSAGE('You are on the first line of the file.');
ELSE
:line_no := :line_no - 1;
TEXT_IO.FCLOSE(variable_declarations.infile);
/* Close the file then reopen it with the
ECHO_TEXT_IO package. This resets the
file pointer. */
ECHO_TEXT_IO;
END IF;

4. Name WRITE_LINE_TO_FILE
Label WRITE CURRENT LINE TO Output FILE

Associated button trigger:
Name WHEN-BUTTON-PRESSED
Trigger Text:
IF :output_file_specs IS NOT NULL THEN
write_to_file;
ELSE
MESSAGE('Must enter a valid output file name with
complete path');
END IF;


Create the following program units and package specifications:
1. ECHO_TEXT_IO* (Procedure Body)
PROCEDURE echo_text_io IS
-- infile TEXT_IO.FILE_TYPE; /* Not used in this program */
linebuf VARCHAR2(300);
line_counter INTEGER;
BEGIN
line_counter := 0;
/*
Note that the IF condition would be redundant if you use
a local variable (commented out above) to save the file pointer.
Infile is a local variable, so the binding would be lost each
time you exited the program. The use of package variables
to act as global file type handles resolves this problem.
*/
IF TEXT_IO.IS_OPEN(variable_declarations.infile) THEN
/*
If the file is open, read in the next line.
*/
TEXT_IO.GET_LINE(variable_declarations.infile, linebuf);
line_counter := line_counter + 1;
ELSE
/*
The file is closed for the application when you display the
previous line; see the WHEN_BUTTON_PRESSED trigger on the
PREVIOUS item. Since the file pointer cannot move
backwards, you must reset the file pointer and loop to the
specified line.
*/
variable_declarations.infile := TEXT_IO.FOPEN(:INPUT_FILE_SPECS, 'r')
;
-- variable_declarations.infile := TEXT_IO.FOPEN('c:\autoexec.bat', 'r')
;
/* You can also hard-code the value */
WHILE line_counter < :line_no LOOP
TEXT_IO.GET_LINE(variable_declarations.infile, linebuf);
line_counter := line_counter + 1;
END LOOP;
END IF;
:line_read := linebuf;
EXCEPTION
WHEN NO_DATA_FOUND THEN
MESSAGE('End Of File reached.');
:line_no := :line_no - 1;
WHEN OTHERS THEN
MESSAGE('UNHANDLED EXCEPTION RAISED in ECHO_TEXT_IO
procedure - Check the file name');
MESSAGE('U. E. R.');
END;

2. VARIABLE_DECLARATIONS (Package Spec)
PACKAGE variable_declarations IS
/*
The following declarations are public and, hence, visible
to the Forms application.
*/
infile TEXT_IO.FILE_TYPE;
outfile TEXT_IO.FILE_TYPE;
line_number NUMBER;
END;

3. WRITE_TO_FILE* (Procedure Body)
PROCEDURE write_to_file IS
BEGIN
IF NOT TEXT_IO.IS_OPEN(variable_declarations.outfile) THEN
variable_declarations.outfile := TEXT_IO.FOPEN(:OUTPUT_FILE_SPECS, 'a');
END IF;
TEXT_IO.PUT_LINE(variable_declarations.outfile, :line_read);
END;


-------------------------------------------------------------------------------
Oracle WorldWide Customer Support

رابط هذا التعليق
شارك

انضم إلى المناقشة

يمكنك المشاركة الآن والتسجيل لاحقاً. إذا كان لديك حساب, سجل دخولك الآن لتقوم بالمشاركة من خلال حسابك.

زائر
أضف رد على هذا الموضوع...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   تمت استعادة المحتوى السابق الخاص بك.   مسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

جاري التحميل
×
×
  • أضف...

برجاء الإنتباه

بإستخدامك للموقع فأنت تتعهد بالموافقة على هذه البنود: سياسة الخصوصية