Overview of a Pro * C program:
1. What is a Pro * C program?
There are three methods to access the database in the ORACLE database management and system;
(1) use SQL * Plus, which has SQL commands to access the database through interactive applications;
(2) applications developed by application development tools in the fourth generation language access the database. These tools include SQL * Froms, QL * Reportwriter, and SQL * Menu;
(3) access using SQL language or ORACLE database function calls embedded in the third generation language.
Pro * C is one of the third development tools. It perfectly integrates Procedural Language C with non-procedural language SQL,
It has complete process processing capabilities and can complete the processing product tasks of any database, so that users can complete various types of reports through programming. The SQL language can be embedded in the Pro * C program,
These SQL languages allow you to dynamically create, modify, and delete tables in a database, and query, insert, modify, and delete rows in a database table, you can also commit and roll back transactions.
PL/SQL blocks can be embedded in Pro * C programs to improve application performance, especially in network environments, which can reduce the total overhead of network transmission and processing.
2. program structure of Pro * C
In general, the Pro * C program is actually a C program embedded with SQL statements or PL/SQL blocks, so its composition is very similar to the C program. But because it is embedded with SQL statements or PL/SQL blocks,
So it also contains different ingredients. In order to give everyone a perceptual knowledge of Pro * C, the differences between the two are as follows:
Full-process variable description of C
C source function 1: Same as function K.
Function 2: Same as function K.
C local variable description
Function K
Executable statement
Description of the external variables of application header C
External description section (ORACLE variable description)
Communication zone description
Pro * C source function 1: Same as function K.
Function 2: Same as function K.
C local variable description
Internal description section of the program body
Communication zone description
Executable Statement of function K C
Executable SQL statements
Or PL/SQL Block
Ii. Structure of the Pro * C program
Each Pro * C program consists of two parts: (1) application header; (2) Application body
The application first defines the relevant variables of the ORACLE database,
Prepare for operating ORACLE databases in C language. The application body is basically composed of SQL statement calls of Pro * C. Queries SELECT, INSERT, UPDATE, DELETE, and other statements.
Application Composition Structure:
Application Header
Description
SQL Communication zone
Application body
EXEC SQL BEGIN DECLARE SECTION
(Definition of SQL variables)
Exec SQL END DECLARE SECTION;
Exec SQL INCLUDE SQLLA;
Exec SQL CONNECT: <User Name>
Identified by: <password>
Use of SQL statements and cursors
1. application header
The first part of the application is the start part of Pro * C. It includes the following three parts:
L C variable description;
L description of SQL variables (declare );
L SQL Communication zone.
(1). Declare (description)
The description section describes the SQL variables of the program. The definition section starts with exec SQL begin declare section, and starts with exec SQL end declare.
Section; ended. It can appear in the Main Department of the program or in the local
L description and use of SQL Variables
The data types that can be specified for SQL variables in the description section are shown in the table:
Data Type description
Char
Char (N)
Int
Short
Long
Float
Double
Varchar Single Character
N character array
Integer
Short integer
Single-precision floating point number
Double-precision floating point number
Variable-length string
These data types are actually C data types, in which varchar is regarded as an extension of the C data type. This will be discussed later.
Pay attention to the following points when using SQL variables:
L must be clearly defined in the description Section
L use the same case format as its definition
L when using an SQL statement, you must add a ":" (colon) before it, but you do not need to add a colon in the C statement.
L cannot be a reserved word in SQL commands.
L can contain directive variables.
Example: exec SQL BEGIN DECLARE SECTIONS;
VARCHAR programe [30];
Int porgsal, pempno;
Exec SQL END DECLARE SECTION;
Exec SQL SELECT ENAME, SAL
INTO: programe,: progsal
FROM EMP
Where empno =: pempno;
(2). Description and reference of indicator variables
The indicator variable is actually a type of SQL variable, which is used to manage the host variables associated with it (that is
When the input or output variable ). Each host variable can define an indicator variable, which is mainly used to process NULL values)
The description of indicator variables is basically the same as that of common SQL variables, but must be defined as a 2-byte integer, such as SHORT and INT. When using SQL statements,
":" (Colon) should also be added before it, and must be attached to its associated host variable, which can be used independently in the C statement. When the indicator variable is-1, it indicates a null value. For example:
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 );
Specifically, ind-num is the indicator variable of dept-number. When the input dept-number value is 0, a null value is inserted into the DEPTNO column of the DEPT table.
(3) Description and use of pointer SQL Variables
Pointer SQL variables must also be in the DECLARE
This section first describes. The format is the same as that of C. When using an SQL statement, you must add the prefix ":" (colon) before the pointer name without the asterisk ). In a C statement, it is used as a pointer variable in C.
(4) Description and reference of array SQL changes
When referencing an array in an SQL statement, you only need to write the array name (add a colon Before the name), and no subscript is required. In a C statement, it is used as an array variable in C language.
Array can greatly reduce network transmission overhead. If you want to insert 100 rows of data to a table without an array, you must repeat the data for 100 times. After referencing the data, you only need to execute one insert statement to insert the data at a time. For example:
Exec SQL BEGIN DECLARE SECTION;
Int emp_number [2, 100];
Char emp_name [100] [15];
Float salary [100], commission [100];
Int dept_number;
Exec SQL END DECLARE SECTION;
....
Exec SQL SELECT EMPNO, ENAME, SAL, COMM
INTO: emp_number,: emp_name,: salary,: commission
FROM EMP
Where deptno =: dept_number;
Pay attention to the following points when using Arrays;
L pointer array not supported
L only supports one-dimensional arrays, and emp-name [100] [15] is regarded as a one-dimensional string.
L The maximum dimension of the array is 32767.
L when multiple arrays are referenced in an SQL statement, these arrays have the same dimension.
L simple SQL variables and array SQL variables cannot be mixed in VALUES, SET, INTO, or WHERE subnames.
L arrays cannot be initialized in the DELARE section.
For example, the following reference is invalid.
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
The VARCHAR variable must be described in the description section before being referenced.
Length, for example:
Exec SQL BEGIN DECLARE SECTION;
Int book-number;
VARCHAR book-name [50];
Exec SQL END DECLARE SECTION;
During preparation, book-name is translated into a structure variable in C language;
Struct {unsigned short len;
Unsigned chart arr [20];
} Boo-name
From this we can see that the VARCHAR variable is actually a structural variable containing the length members and the number of members in the group. When using an SQL statement, the structure name prefixed with a colon should be referenced, and the structure member should be referenced in the C statement without subscript.
When the VARCHAR variable is used as the output variable, it is automatically set by ORACLE. When used as the input variable, the program should first store the string into the array member,
The length is stored in the length member and then referenced in the SQL statement. For example:
Main ()
{.......
Scanf ("90 s, 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 zone
The SQL Communication zone is described using the following statements:
Exec SQL INCLUDE SQLCA;
This section provides records of success or failure and error handling for running programs.
SQLCA Composition
SQLCA is a variable of the structure type. It is an interface between ORACLE and applications. ORACLE
Store the status information of each embedded SQL statement execution in SQLCA. Based on this information, you can determine whether the SQL statement execution is successful, the number of rows processed, and the error information, as shown in the table:
Struct sqlca
{Char sqlcaid [8]; ---- à identifies the communication zone
Long sqlabc; --- à length of the Communication zone
Long sqlcode; --- à keep the status code of the recently executed SQL statement
Struct {unsigned short sqlerrml; ----- à message text Length
} Sqlerrm;
Char sqlerrp [8];
Long sqlerrd [6];
Char sqlwarn [8];
Char sqlext [8];
}
Struct sqlca;
Among them, sqlcode is the most commonly used in programs. It retains the status code of the recently executed SQL statement. The programmer processes the status code accordingly. The status code values are as follows:
0: indicates that the SQL statement is correctly executed without errors or exceptions.
> 0: ORACLE executes this statement, but encounters an exception (if no data is found ).
<0: indicates that the SQL statement is not executed by ORACLE due to database, system, network, or application errors.
When such errors occur, the current transaction should generally be rolled back.
2. Application body
In the Pro * C program, SQL statements and C statements can be freely written together and SQL variables can be used in SQL statements. The syntax for writing embedded SQL statements is:
L start with the keyword EXEC SQL
L end with a statement terminator (semicolon) in C Language
SQL statements are mainly used to deal with databases. C language programs are used for control, input, output, and data processing.
(1) connect to the ORACLE database
Before accessing the database, you must connect the program to the ORACLE database. Log on to ORACLE. The connected command should be the first executable command of the application. The connection command format is as follows:
Exec SQL CONNECT: <User Name> IDENTIFIED BY: <password>
Or exec SQL CONNECT: <User Name>/<password>
When using the preceding two formats for login, you must first define
SQL variables, and set them before running CONNECT. Otherwise, the logon will fail. For example:
Exec SQL BEGIN DECLARE SECTION;
VARCHAR usename [20];
VARCHAR password [20];
EXEC SQL END DECLARE
..........
Strcpy (usename. 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 indntified by: password;
Note: the user name and password cannot be directly written into the connect statement, or the strings enclosed by quotation marks (') are in the connect statement. The following statement is invalid.
Exec SQL connect Scott inentified by tiger;
Exec SQL connect 'Scott 'identified by 'tiger ';
(2). insert, update, and delete
I have already discussed the SQL language in detail. I will not illustrate it here.
(3) Use of database queries and cursors
In Pro * C, there are two types of queries:
L returns a query of a single row or a specified number of rows;
L multi-row query is returned. This query requires the use of a cursor to control each row or each group (the main variable uses an array ).
1) returns a query of a single row or a specified number of rows.
The SQL SELECT statement in Pro * C consists of the following clauses:
Select
Into
From
Where
Connect
Union
Intersect
Minus
Group
Having
Order
The query conditions in the WHERE clause can be a set of one or more attributes, and the primary variables that are assigned values during execution can also be placed in the WHERE clause. the primary variable used in the WHERE clause is called the input primary variable. For example:
Select empno, JOB, SAL
INTO: PNAME,: PJOB,: PSAL
FROM EMP
Where empno =: PEMPNO;
If no row is found, SQLCA. SQLCODE returns "+ 1403", indicating "not found ".
The main variable in the INTO clause is called the output main variable, which provides the information required for query.
Before any item is sent to the primary variable, ORACLE is required to convert these items into the data type of the primary variable. Digits are truncated (for example, 9.23 is converted to 9 ).
If only one row is returned for a query, you do not need to use a cursor. You only need to add an INTO clause to the SELECT statement. In terms of semantics, The INTO statement has multiple primary variables output in the query before the FROM statement. If the number of expressions in the SELECT clause is not equal to the number of primary variables in the INTO clause, set SQLCA. SQLWARN [3] To "W ".
2) use of multi-row queries and cursors
If the query returns multiple rows or does not know how many rows are returned, use the SELECT statement with an oracle cursor (CURSOR.
A cursor is a work area where ORACLE and PRO * C store query results. A cursor (named) is associated with a SELECT statement. The operation cursor has four commands: (1) DECLARE
CURSOR; (2) open cursor; (3) FETCH; (4) close cursor.
A. Define A cursor
A cursor must be defined before it can be used. Syntax:
Exec SQL DECLARE <cursor Name> CORSOR
SELECT <column> 〉
FROM <Table> 〉
For example:
Exec SQL DECLARE CSOR, CURSOR
Select ename, JOB, SAL
FROM EMP
Where deptno =: DEPTNO;
When a CURSOR associated with the query is assigned, multiple rows can be returned from the database when the SELECT statement queries the EMP. These rows are an active area of the CURSOR.
Note:
1) defining a cursor must be completed before the cursor operation;
2) PRO * C cannot reference unspecified cursors;
3) after a cursor is defined, its scope of action is the entire program. So it is wrong for a program to define two identical cursors at the same time.
B. Open the cursor.
The OPEN statement used to OPEN a cursor is mainly used to input the content of the primary variable. These are the primary variables used in the WHERE clause. The statement for opening a cursor is exec SQL OPEN <cursor Name> 〉
When the cursor is enabled, you can retrieve more than one row of results from the related query. All rows that meet the query criteria form a set called a "cursor activity set ". Through the fetch operation, each row or group in the activity set returns one by one. After the query is complete,
The cursor can be closed. :
Define the cursor: DECLARE
Start query: SELECT
OPEN cursor: OPEN
FETCH data from activity set: FETCH
Query completed
CLOSE cursor: CLOSE
Note: 1) the cursor is in front of the first row of the active set;
2) If the input main variable is changed, the cursor must be reopened.
C. Fetch data
The process of retrieving a row or a group of results from an activity set to the output primary variable is called data retrieval. The output primary variable is defined in the Data fetch statement. The data retrieval statement is as follows:
Exec SQL FETCH <cursor Name> INTO: primary variable 1, primary variable 2 ,......
FETCH process:
Query Result
Cursor
FETCH
Query Result
After the cursor is opened
Output to current
......
The query result is the query result that meets the query conditions. Pay attention to the following points when using FETCH:
L The cursor must be defined before being opened.
L The FETCH statement is executed only after the cursor is opened.
L
Each time a FETCH statement is executed, data is retrieved from the current row or the current group once, And the next row or the next group is moved up once. Each row or group referred to by a cursor is the current row or group, and each FETCH is the data of the row or group specified by the cursor.
L when the cursor activity set is empty, ORCLE returns a SQLCA. SQLCA (= 1403 ).
L if you want this cursor to be operated again, you must close it before opening it.
L a memory space can be opened up in the C program to store the operation results, so that the opened space can be used to flexibly manipulate the query results.
D. Close the cursor.
After all rows in the active set are retrieved, you must close the cursor to release resources related to the cursor.
The format of closing the cursor is:
Exec SQL CLOSE cursor name;
For example:
Exec SQL CLOSE C1;
ORACLE V5.0 supports the SQL format "CURRENT OF CURSOR ". This statement points to the last row retrieved from a cursor,
For modification and deletion. This statement must be used after a get operation. It stores a ROWID and uses it.
(4). Example
EXEC SQL DECLARE SALESPEOPLE CURSOR
Select ssno, NAME, SALARY
FROM EMPLOYEE
Where dname = 'sales ';
Exec SQL OPEN SALESPEOPLE;
EXEC SQL FETCH SALESPEOPLE
INTO: SS,: NAME,: SAL;
Exec SQL CLOSE SALESPEOPLE;
(5) SQL nesting Methods and Applications
Embedded SQL and interactive SQL have the following differences in form:
1) Add the prefix "exec SQL" before the SQL statement, which is easy to recognize during pre-compilation,
Each SQL statement can be processed as a high-level language.
2) Each SQL statement is divided into two categories: descriptive statements and executable statements. Executable statements can be divided into four categories: Data Definition, data control, data manipulation, and data retrieval.
Executable SQL statements are written in the executable area of the advanced language, and descriptive SQL statements are written in the descriptive area of the advanced language.
For example, to create a table structure named BOOK in the PRO * C program, the process is as follows:
# Include <stdio. h> 〉
Exec SQL BEGIN DECLARE SECTION;
VARCHAR uid [20], pwd [20];
Exec SQL END DECLARE SECTION;
Exec SQL INCLUDE SQLCA;
Main ()
{
/* Login database */
Strcpy (uid. arr, 'wu ');
Uid. len = strlen (uid, arr );
Strcpy (pwd. arr, 'wu ');
Pwd. len = strlen (pwd. arr );
Exec SQL CONNECT: uid IDENTIFEED BY: pwd;
Exec SQL CREATE TABLE book
(Acqnum number, copies number, price number );
Exec SQL COMMIT WORK RELEASE;
EXIT;
PRO * C can easily and flexibly access the data in the ORCLE database and features high-speed C language. Therefore, it can complete tasks that cannot be completed by ORACLE products, for example, output results in the next fixed special format.
SQL nested source code example
# Unclude
Typedef char asciz [20];
Exec SQL BEGIN DECLARE SECTION;
Exec SQL TYPE asciz IS STRING (20) REFERENCE;
Asciz username;
Asciz password;
Asciz emp_name (5 );
Int emp_number (5a );
Float salary [5];
Exec SQL end declare section;
Exec SQL include sqlca;
Void print_rows ();
Void sqlerror ();
Main ()
{
Int num_ret;
Strcpy (username, "Scott ');
Strcpy (password, "Tyger ");
Exec SQL whenever sqlerror do sqlerror ();
Exec SQL CONNECT: username identified by: password;
Print ("/nconnected to Oracle as user: % s/n", username );
Exec SQL declare C1 cursor
Select empno, ename, Sal from EMP;
Exec SQL open C1;
Num_ret = 0;
For (;;)
{
Exec SQL whenever not found do break;
Exec SQL fetch C1 into: emp_number,: emp_name,: salary;
Print_rows (sqlca. sqlerrd [2]-num_ret );
Num_ret = sqlca. sqlerrd [2];
}
If (sqlca. sqlerrd [2]-num_ret)> 0 );
Print _ rows (sqlca. sqlerrd [2]-num_ret );
Exec SQL CLOSE c1;
Printf ("/Have a good day./n ");
Exec SQL COMMIT WORK RELEASE;
}
Void print_rows (n );
Int n;
{
Int I;
Printf ("/nNumber Employee Salary/n ");
Printf ("------------------------------/n ");
For (I = 0; I Printf ("%-9d %-8 s % 9.2f/n", emp-number [I], emp --- name [I], salary [I];
}
Void sqlerror ()
{
Exec SQL WHENEVER SQLERROR CONTINUE;
Printf ("/noracle error detected:/n ");
Printf ('/n %. 70 s/n ", sqlca. sqlerrm. sqlerrmc );
Exec SQL ROLLBACK WORK RELEASE;
Exit (1 );
}
(6) error detection and recovery
When using SQL statements and PRO * C to operate databases, null fields, unconditional deletion, row-free return, data overflow, and truncation often occur, this phenomenon can be detected using SQLCA and indicator variables.
1 SQLCA Structure
In the PRO * C program, the SQLCA structure is as follows:
Struct sqlca {
Char sqlcaid [8];
Long sqlabc;
Long sqlcode;
STRUCT {
Unsigned sqlerrm1;
Char sqlerrmc [10];
} Sqlerrm;
Char sqlerrp [8];
Long sqlerrd [6];
Char sqlwarn [8];
Char sqlext [8];
}
Where:
1) SQLCA. sqlerrm. sqlerrmc: With SQLCA. SQLCODE error body.
2) SQLCA. sqlerrd: The current ORACLE status, which is meaningful only to SQLCA. SQLERRD [2] and indicates the number of rows processed by the DML statement.
3) SQLCA. sqlwarn: Provides information about possible conditions.
After each SQL statement is executed, ORACLE puts the returned results into SQLCA, except for the description statements.
You can use SQLCA to view the execution results of SQL statements. There are usually three results:
= 0: Execution successful;
SQLCA. SQLCODE => 0: status value of successful execution;
<0: Failed. execution cannot be continued.
2 indicator variables
An indicator variable is also called an indicator variable. It indicates that the variable is associated with a primary variable and indicates the response of the primary variable.
= 0: the return value is not empty and is not truncated. The value is placed in the main variable;
Return Value => 0: the return value is null. Ignore the value of the main variable;
<0: if the length of the main variable is insufficient, it is truncated.
Note the following when using indicator variables:
L indicator variables cannot be used in the WHERE clause. Use the null attribute to test the null value.
For example, the following clause:
Select...
From...
Where ename is null;
Is correct, and
Where ename =: peme: peme1
Yes.
L indicates that the variable is-1 before the null value is inserted.
L can output null values.
3 whenever statement
Whenever is a description statement that does not return sqlcode. It only specifies relevant measures based on the return code in sqlca. Format:
Exec SQL whenever [sqlerror | sqlwarning | notforund]
[Stop | continue | goto <number>];
Where
(1) The default value of [Stop | continue | got <label>] is continue.
(2) sqlerror: sqlca. sqlcode <0;
(3) sqlwarnign: sqlca. sqlwarn [0] = "W ";
(4) NOTFOUND: SQLCA. SQLCODE = 1403;
The following is a program to describe WHENEVER usage:
Exec SQL BEGIN DEELARE SECTION;
Varchar uid [20];
Varchar pasw [20];
......
Exec SQL END DECLARE SECTION;
Exec SQL INCLUDE SQLCA;
Main ()
{
......
Exec SQL WHENEVER SQLERROR GOTO ERR;
Exec SQL CONNECT: UID/: PWD;
......
Exec SQL DECLARE CSOR1 CURSOR
SELECT <field> 〉
FORM <Table> 〉
Exec SQL OPEN CSOR1;
SQL
......
Exec SQL WHENEVER NOT FOUND GOTO good;
For (;;)
Exec SQL FETCH CSOR, ......
Good:
......
Printf ("/n query end/n ");
Exec SQL CLOSE C1;
Exec SQL WHENEVER SQLERROR CONTINUE.
Exec SQL COMMIT WORK RELEASE:
Exit ();
Printf ("/n % 70s | n", sqlca. sqlerrm. sqlerrmc );
Exec SQL ROLLBACK WORK RELEASE:
Exit (1 );
}
(7) Dynamic Definition Statement
SQL statements include dynamic and static definition statements:
(1) static definition statement: the SQL statement is pre-compiled into PRO * C and the Target Program * is formed after pre-compiler compilation *. BOJ, and then run the program Pre-run.
(2) Dynamic Definition Statement: some statements cannot be embedded in the PRO * C program beforehand. You must enter the device (such as the terminal) according to the program running status) enter the SQL statement to be executed in real time.
The dynamic definition statements include:
L execute immediate;
L PREPARE and EXECUTE;
L PREPARE, FETCH, and OPEN;
L BIND and define descriptor.
1. execute immediate statement
This statement indicates immediate execution and only returns the execution result to SQLCA without any other information. For example:
Exec SQL BEGIN DECLARE SECTION;
VARCHAR abcd [89];
VARCHAR deay [20];
Exec SQL END DECLARE SECTION;
/** Output string to abcd **/
Exec SQL EXECUTE IMMEDIATE: abcd;
Note:
1) execute immediate can only run dynamic statements with one parameter. Here, abcd is a parameter, not a keyword.
2) the prerequisite for execute immediate is that SQL statements cannot contain primary variables, and SQL statements cannot be query statements.
3) any primary variable can be used as the parameter of execute immediate, or a string can be used as the primary variable.
2. PREPARE and EXECUTE statements
This statement indicates "pre-compilation/execution ". This statement can be pre-compiled once and executed multiple times. Syntax:
Exec SQL PREPARE <statement Name> FROM: Main variable;
Exec SQL EXECUTE <statement Name> [USING: replace primary variables];
The PREPARE statement does two things:
(1) pre-compile SQL statements;
(2) Name of the SQL statement.
Note:
L The SQL statement cannot be a query statement;
L PREPARE and EXECUTE can contain primary variables;
L PREPARE cannot be executed multiple times.
For example:
# Define USERNAME "SCOTT"
# Define PASSWORD "TIGER"
# Include
Exec SQL INCLUDE sqlca;
Exec SQL BEGIN DECLARE SECTION;
Char * username = USERNAME;
Char * password = PASSWORD;
VARCHAR sqlstmt [80];
Int emp_number;
VARCHAR emp_name [15];
VARCHAR job [50];
Exec SQL END DECLARE SECTION;
Main ()
{
Exec SQL WHENEVER SQLERROR GOTO: sqlerror;
Exec SQL CONNECT: username IDENTIFIED BY: password;
Sqlstmt. len = sprintf (sqlstmt. arr, "insert into emp (EMPNO, ENAME, JOB, SAL)
VALUES (: V1,: V2,: V3,: V4 )");
Puts (sqlstmt. arr );
Exec SQL PREPARE S FROM: sqlstmt;
For (;;)
{
Printf ("/nenter employee number :");
Scanf ("% d", & emp_number );
If (emp_number = 0) break;
Printf ("/nenter employee name :");
Scanf ("% s", & emp_name.arr );
Emp_name.len = strlen (emp_name.arr );
Printf ("/nenter employee job :");
Scanf ("% s", job. arr );
Job. len = strlen (job. arr );
Printf ("/nenter employee salary :");
Scanf ("% f", & salary );
}
Exec SQL EXECUTE S USING: emp_number,: emp_name,: job,: salary;
}
3. FETCH and OPEN statements
The dynamic statements of FETCH and OPEN are used to operate the cursor. the execution process is as follows:
PREPARE <statement Name> FROM <main variable string> 〉;
DECLARE <cursor Name> FOR <statement Name> 〉;
OPEN <cursor Name> [USING: replace variable 1 [,: replace variable…]
FETCH <cursor Name> INTO: primary variable 1 [,: primary variable 2…]
CLOSE <cursor Name> 〉
Note:
L SQL statements allow the use of query statements;
L The column names in the SELECT clause cannot be changed dynamically and can only be preset;
L The WHERE and order by clauses can dynamically change conditions.
1. Compilation and running of Pro * C
1.
First, use the ORACLE pre-compiler PROC to pre-process the PRO * C program. This compiler translates the SQL language embedded in the source program into C language and generates a file that can be directly compiled by the C language compiler. The file extension is
. C
2. Use c language compiler CC to compile a file with the extension. c to generate a target code file with the extension. o
3. Use the MAKE command to connect to the target code file and generate the executable file.
For example, compile and run the preceding example. pc.
PROC iname = example. pc
CC example. c
Make exe = example OBJS = "example. o"
Example