1. Program Structure
Each PRO*C program consists of two parts: (1) The application header, and (2) the application body
The application header defines the variables for the Oracle database and prepares to manipulate the Oracle database in the C language. The application body is basically made up of PRO*C SQL statement calls.
The main point is to query Select, INSERT, UPDATE, delete and other statements.
The composition of the application:
2. Function cases
EXEC SQL INCLUDE sqlca; #include"My_sys.h"#include"errlog.h"/*connecting to a database*/intOPENDB (CharDb_type) {EXEC SQL BEGIN DECLARE section; Charsauser[ - ]; Charsapasswd[ - ]; Charsatnsname[ - ]; EXEC SQL END DECLARE section; /*********** * Online database ************/strcpy (Sauser, getenv ("Oracleuserid")); strcpy (SAPASSWD, getenv ("oracleuserpwd")); strcpy (Satnsname, getenv ("Oracletnsname")); EXEC SQL Connect:sauser identified by:sapasswd using:satnsname; if(Sqlca.sqlcode! =0) {printf ("db not open[%d]\n", Sqlca.sqlcode); return-1 ; } return 0;}/*Reconnect the database*/intreopendb () {intTry_num =0, ret; Htlog (ERROR,"DB down\n"); Closedb (); while(1) {printf ("Connect to DB%dth\n", Try_num); RET= Opendb ('L'); if(ret = =0) {printf ("Connect to DB succ\n"); return 0; } closedb (); Sleep ( -); Try_num++; if(Try_num > the) Break ; } printf ("db not open\n"); return-1 ;}/*Close the database*/intClosedb () {EXEC SQL COMMIT work RELEASE; return 0 ;}/*Transaction Rollback*/intDbsrollback () {EXEC SQL ROLLBACK work; return 0;}/*Transaction Commit*/intDbscommit () {EXEC SQL COMMIT; return 0;}
Description
You need to configure the Oracleuserid, ORACLEUSERPWD, oracletnsname three environment variables in. BASRC before running. Where the printf part can be changed to T_log, E_log output
pro*c function Case 1--database connection, transaction processing