Use of Pro * C
An Overview of Pro * C Program:
1. What is Pro * C program
In Oracle Database Management and System, there are three ways to access the database;
(1) With SQL * Plus, it has the SQL command to access the database with an application for interaction;
(2) Use the application of the fourth-generation language application development tool to access the database, these tools have SQL * froms, QL * ReportWriter, SQL * MENU, etc .;
(3) Use the SQL language or Oracle library function calls embedded in the third-generation language.
Pro * c belongs to one of the third development tools, which combines process-based language C and non-processed language SQL, with complete process processing capabilities, and completes any database processing tasks, users Various types of reports can be completed by programming. In the Pro * C program, you can embed the SQL language. Use these SQL languages to complete dynamically, modify, and delete the tables in the database, you can also query, insert, modify, and delete lines in the database table, you can also implement transaction And roll back.
In the Pro * C program, the PL / SQL block can also be embedded to improve the performance of the application, especially in a network environment, can reduce the total overhead of network transmission and processing.
2. Pro * c programming
Popular, the Pro * C program is actually embedded with a SQL statement or a PL / SQL block, so its composition is similar to the C program. But because of its embedded SQL statement or PL / SQL block, it also contains different ingredients. In order to let everyone have an inductive understanding of Pro * C, the difference is as follows:
C's full variable description
C Source Program Function 1: Tong Function K.
Function 2: That with functions K.
C Partial Variable Description
Function K
Executable statement
Application header C's external variable description
External Description (Oracle Variable Description)
Communication area description
Pro * C Source Process 1: With Function K.
Function 2: That with functions K.
C local variable description
Interior of the program, some internal description segments
Communication area description
Performance statement of function K C
Executable statement of executable statement SQL
Or PL / SQL block
two. Composition of Pro * C Programs
Each Pro * C program includes two parts: (1) application header; (2) application body
The application first defines the relevant variables of the Oracle database, and prepares for manipulating the Oracle database in the C language. The application body is basically called by Pro * C SQL statement. Mainly refer to SELECT, INSERT, UPDATE, DELETE and other statements.
The composition structure of the application is shown in the figure:
Application first
Description section
SQL communication area
Application program
EXEC SQL Begin Declare Section
(Definition of SQL variable)
Exec SQL End Declare Section;
EXEC SQL include Sqlla;
EXEC SQL Connect:
Identified by:
SQL statement and cursor use
1. Application header
The first part of the application is the beginning of Pro * C. It includes three parts: L C variable description section;
l SQL variable description section (Declare part);
l SQL communication area.
(1). Declare part (description section)
Description section describes the SQL variable of the program, the defined portion is ended with Exec SQL Begin Declare Section; ending with EXEC SQL End Declare Section; It can appear in the main sector of the program, can also appear on topical
l SQL variable description and use
The data type specified by the segment can be specified for the SQL variable is shown in the table:
type of data
description
Charr
Char (n)
Int
Short
Long
Float
Double
VARCHAR
Single character
N character arrays
Integer
Short integer
Single precision floating point
Double precision floating point
Growth string
These data types are actually the data type of C language, where varchar is expanded to the C data type. This is talking later.
The use of SQL variables should be aware of the following points:
l must be clearly defined in the description
l Must use the same case in which it is defined.
l When you are used in the SQL statement, you must add a ":" (colon) before it, but you don't need to colon when you are referenced in the C statement.
l Can't be the reserved word in the SQL command.
l You can take an indication variable.
For example: Exec SQL Begin Declare Sections;
Varchar Programe [30];
INT PORGSAL, PEMPNO;
Exec SQL End Declare Section;
Exec SQL SELECT ENAME, SAL
INTO: Program,: Progctions
From EMP
WHERE Empno =: pempno;
(2). Description and reference of the indicator variable
The indication variable is actually a class of SQL variables that are used to manage host variables associated therewith (ie, the variables that are input or output in the SQL statement). Each host variable can define an indicator variable, mainly for processing null values (NULL)
The instructions of the indicator variable are basically the same as general SQL variables, but must define 2 bytes of integers, such as short, int. When referenced in the SQL statement, it should also be added ":" (colon), and must be attached to the associated host variable, can be used independently in the C statement. When the indicator variable is -1, a null value is indicated. E.g:
EXEC SQL Begin Declare Section;
INT DEPT- NUMBER;
Short Ind - Num;
Char Emp -Name;
Exec SQL End Declare Section;
Scanf ("90D% S", & DEPT- Number, DEPT - NAME);
IF (DEPT - Number == 0)
IND - NUM = -1;
Else
IND - NUM = 0;
Exec SQL INSERT INTO DEPT (DEPTNO, DNAME)
VALUES (: DEPT - NUMBER: IND- NUM,: DEPT - NAME);
Where IND-NUM is the indicator variable of DEPT - NUMBER. When the input DEPT-Number value is 0, an empty value is inserted to the DEPTNO column of the DEPT table.
(3). Description and use of the pointer SQL variable
The pointer SQL variable must also be described in the DECLARE section before reference. It illustrates the format as C language. When referenced in the SQL statement, the pointer is prefixed before the name is ":" (colon) without adding "*". Usage in the C statement like a pointer variable in the C language. (4). Description and reference for array SQL change
When references the array in the SQL statement, just write an array name (before the name before the name), do not need to write the subscript, the usage in the C statement is like the array variables in the C language.
Using an array can greatly reduce network transmission overhead. To insert 100 row data to a table, if there is no array, you must repeat 100 times, and after reference, you only need to execute an insert statement will insert it. E.g:
EXEC SQL Begin Declare Section;
INT EMP_NUMBER [100];
Char EMP_NAME [100] [15];
FLOAT SALY [100], CommsSion [100];
INT dept_number;
Exec SQL End Declare Section;
.
Exec SQL SELECT Empno, ENAME, SAL, COMM
INTO: EMP_NUMBER,: EMP_NAME,: SALARY,: CommIions
From EMP
WHERE deptno =: dept_number;
When using arrays, pay attention to the following points;
l Does not support the argument array
l Only one-dimensional array, and EMP-NAME [100] [15] is deemed to be a one-dimensional string
The maximum number of ports in the L is 32767
l When references multiple arrays in a SQL statement, these array dimensions should be the same.
l In values, set, into, or where subsets, not allowing simple SQL variables to mix with array SQL variables.
l Can't initialize the array in Delare part
For example: The following reference is illegal
EXEC SQL Begin Declare Section;
INT DEPT - NUM [3] = {10, 20, 30};
Exec SQL End Declare Section;
Exec SQL SELECT Empno, ENAME, SAL
INTO: EMP - NUM [I],: Emp - Name [i],: salarg [i]
From EMP
(5) Description and reference of pseudo type varchar
VARCHAR variables must also be described in the description before reference, and the maximum string must be pointed out.
Length, such as:
EXEC SQL Begin Declare Section;
INT book - Number;
VARCHAR BOOK - NAME [50];
Exec SQL End Declare Section;
When prepared, Book - Name is translated into a structural variable in C language;
Struct {UNSIGNED SHORT LEN;
UNSIGNED Chart Arr [20];
} Boo - Name
As a result, VarChar variables are actually a structural variables that contain length members and array members. When referenced in the SQL statement, reference should be referenced to the structure name prefixed with a colon, and the standard member is referenced in the C statement.
When the VARCHAR variable is automatically set by Oracle, when as an input variable, the program should put the string into an array member, and the length is deposited in the length member, and then reference it in the SQL statement. E.g:
Main ()
{....... SCANF ("90S, 90D ', BOOK - Name .arr, & Book - Number);
Book - name .len = strlen (Book - name .arr);
EXEC SQL UPDATE BOOK
SET BNAME =: Book - Name;
BDESC =: BOOK - NUMBER;
}
(6) SQL communication area
The SQL communication area is described in the following statement:
EXEC SQL include Sqlca;
This section provides the failure or failure of the user running program.
SQLCA composition
SQLCA is a structural type variable that is an interface of Oracle and an application. When executing the Pro * C program, Oracle stores the status information performed by each embedded SQL statement into SQLCA. According to this information, it can determine whether the execution of the SQL statement is successful, the number of rows, error information, etc., the composition is Down:
Struct Sqlca
{char sqlcaid [8]; ---- à identity communication area
Long SQLABC; --- à The length of the communication area
Long Sqlcode; - à Reserved the status code of the recently executed SQL statement
Struct {unsigned short sqlerrml; ----- à information text length
SQlerrm;
Char sqlerrp [8];
Long SQlerRD [6];
Char sqlwarn [8];
Char sqlext [8];
}
Struct Sqlca SQLCA;
Where SQLCode is most common in the program, it retains the status code of the recently executed SQL statement. The programmer makes the corresponding processing based on these status code. These status code values are as follows:
0: Indicates that the SQL statement is executed correctly, no errors and exceptions.
> 0: Oracle executes this statement, but encounter an exception (if you didn't find any data).
<0: Indicates that Oracle does not execute the SQL statement due to errors of databases, systems, networks, or applications.
When this error occurs, the current transaction should generally be rolled back.
2. Application program
In the Pro * C program, you can freely mix writing SQL statements and C statements, and can use SQL variables in the SQL statement, the writing literacy of embedded SQL statements is:
l Start with keyword EXEC SQL
l Terminal with the C language statement (semicolon)
The role of the SQL statement is mainly used to deal with the database. The C language program is used to control, input, output, and data processing, etc..
(1) Connect to Oracle Database
Before you get the data inventory, you must first connect the program to the Oracle database. Log in to Oracle. The connected command should be the first executable command of the application. The connection command format is as follows:
EXEC SQL Connect:
Or Exec SQL Connect:
When logging in using the two formats described above, you should first define the user name and password first in the description segment.
SQL variables and setting them before performing Connect, otherwise the login failed. E.g:
EXEC SQL Begin Declare Section;
VARCHAR Usename [20];
VARCHAR Password [20];
EXEC SQL END DECLARE
........
STRCPY (useename.arr, "csott ');
Usename.len = Strlen (username.arr);
STRCPY (Password.arr, "Tiger ');
Password .len = Strlen (Password .arr);
EXEC SQL WHENEVER SQLERROR GOTO SQLERR;
Exec SQL Connect: UserName InditiFied by: Password;
Note: You cannot write the username and password to the Connect statement, or the letter string enclosed in quotation marks (') in the Connect statement, as ineffective below.
EXEC SQL Connect Scott inentified by Tiger;
EXEC SQL Connect 'Scott' Identified by 'Tiger'
(2). Insert, update, and delete
After telling the SQL language, it has been speaking, which is not explained here.
(3). Database query and cursor
In Pro * C, the query can be divided into two types:
l Return to a single line or a query;
l Return to multi-line queries. This query requires a cursor to control each row or each group (the main variable array).
1) Return to a single line or fixed number of queries
The query SQL SELECT statement in Pro * C consists of several clauses:
SELECT
INTO
From
WHERE
CONNECT BY
Union
INTERSECT
Minus
GROUP BY
Having
ORDER BY
Where the query condition in the WHERE clause can be a collection of attributes or multiple properties, which can be placed in the WHERE clause in execution. The main variable used in the Where clause is called the input main variable. Such as:
SELECT Empno, Job, Sal
INTO: PNAME,: PJOB,: PSAL
From EMP
WHERE Empno =: pempno;
If you do not find a defined line, Sqlca.sqlcode returns " 1403", indicating that "not found".
The main variable quantity of the INTO clause outputs the main variable, which provides the information required when queries.
Before any item is given to the primary variable, it is required to convert these items to the data type of the main variable. For numbers, it is done by truncation (eg 9.23 converted to 9).
If you have determined that the query only returns a row, you don't have to use a cursor, you can only add an INTO clause to the SELECT statement. How many output primary variables have multiple output main variables in the query in the query in the query in the query in the query in the query. If the number of expressions in the SELECT item is not equal to the number of main variables in the INTO clause, set SQLCA.SQLWARN [3] to "W".
2) Multi-line inquiry and cursor use
If the query returns multiple lines or does not know how many rows returned, use the SELECT statement with the Oracle Cursor (CURSOR).
The cursor is Oracle and Pro * C store the work area of the query results. A cursor (named) is associated with a SELECT statement. Operating cursor has 4 commands: (1) DECLARE CURSOR; (2) Open cursor; (3) fetch; (4) Close Cursor.
A. Defining a cursor
A cursor must first be defined before it can use it. The syntax is:
EXEC SQL DECLARE
Select
From