Pro C Tutorial - Programming Examples
Pro C Tutorial - Programming Examples
Blog
Pro C Tutorial
Active Sections Fold
Table of Contents
Welcome page
Programming Introduction
Computer heading level 1
Certifications How to Compile a Pro*C
Computer Science program
Open Source Pro*C Syntax
Projects SQL
Mathematics for Preprocessor Directives
Computer Science Statement Labels
Operating Systems Host Variables
Parallel Computing Basics
Computer Pointers
Hardware Structures
Databases Arrays
Networks Indicator Variables
Data Mining Datatype Equivalencing
Book Reviews Dynamic SQL
Software Transactions
Engineering Error Handling
SQLCA
WHENEVER Statement
Pages
Recent changes
List all pages
Page Tags
Introduction
Site Manager Embedded SQL is a method of
combining the computing power
Page tags of a high-level language like
C/C++ and the database
manipulation capabilities of SQL.
blog
drupal_todo It allows you to execute any SQL
s
needs_example
qt
todo_cpp
todo_php
heading level 1
programmingexamples.wikidot.com/pro-c-tutorial 1/13
8/17/2021 Pro C Tutorial - Programming Examples
How to Compile a
new page
Pro*C program
new Blog Entry A Pro*C program is compiled in
two steps.
Share on Facebook
First, the Pro*C precompiler
edit this panel recognizes the SQL statements
embedded in the program, and
replaces them with appropriate
calls to the functions in the SQL
runtime library.
proc file.pc
Pro*C Syntax
SQL
All SQL statements need to start
with EXEC SQL and end with a
semicolon ";". You can place the
SQL statements anywhere within
a C/C++ block, with the
restriction that the declarative
statements do not come after the
executable statements.
Example
int a;
/* ... */
Explore »
printf("The salary
/* ... */
Preprocessor Directives
The C/C++ preprocessor
directives that work with Pro*C
are #include and #if. Pro*C does
not recognize #define. For
example, the following code is
invalid:
Example
WHERE SSN = TH
Statement Labels
You can connect C/C++ labels
with SQL.
Example
error_in_SQL:
/* do error handling */
Host Variables
Basics
Host variables are the key to the
communication between the host
program and the database. A
host variable expression must
resolve to an lvalue (i.e., it can
programmingexamples.wikidot.com/pro-c-tutorial 3/13
8/17/2021 Pro C Tutorial - Programming Examples
char
char[n]
int
short
long
float
double
VARCHAR[n] - This is a
pseudo-type recognized by
the Pro*C precompiler. It is
used to represent blank-
padded, variable-length
strings. Pro*C precompiler
will convert it into a
structure with a 2-byte
length field and a n-byte
character array.
Example
int main()
/* ... */
:(*(deptnos+
lvalue */
VALUES(:x, :y,
:get_deptno(
EXEC SQL INSERT INTO em
VALUES(:x, :y,
:(*get_depno
/* ... */
Pointers
You can define pointers using the
regular C syntax, and use them
in embedded SQL statements. As
usual, prefix them with a colon:
Example
int *x;
/* … */
EXEC SQL
SELECT xyz INTO :x
FROM …;
Structures
Structures can be used as host
variables.
Example
typedef struct {
char name[21]; /
programmingexamples.wikidot.com/pro-c-tutorial 5/13
8/17/2021 Pro C Tutorial - Programming Examples
char name[21]; /
site-name .wikidot.com Share on
Explore »
int
SSN;
} Emp;
/* ... */
Emp bigshot;
/* ... */
Arrays
Host arrays can be used in the
following way:
int emp_number[50];
char name[50][11];
/* ... */
Indicator Variables
Indicator variables are essentially
"NULL flags" attached to host
variables. You can associate
every host variable with an
optional indicator variable. An
indicator variable must be defined
as a 2-byte integer (using the
programmingexamples.wikidot.com/pro-c-tutorial 6/13
8/17/2021 Pro C Tutorial - Programming Examples
Example
short indicator_var;
/* ... */
VALUES(:host_var IN
Datatype Equivalencing
Oracle recognizes two kinds of
datatypes: internal and external.
Internal datatypes specify how
Oracle stores column values in
database tables. External
datatypes specify the formats
used to store values in input and
output host variables. At
precompile time, a default Oracle
external datatype is assigned to
each host variable. Datatype
equivalencing allows you to
override this default
equivalencing and lets you
control the way Oracle interprets
the input data and formats the
output data.
char emp_name[21];
VARRAW(4000) REFERENCE;
my_raw buffer;
/* … */
buffer =
malloc(4004);
programmingexamples.wikidot.com/pro-c-tutorial 8/13
8/17/2021 Pro C Tutorial - Programming Examples
site-name .wikidot.com
Dynamic SQL
Share on
Explore »
Transactions
Oracle PRO*C supports
transactions as defined by the
SQL standard. A transaction is a
sequence of SQL statements that
Oracle treats as a single unit of
work. A transaction begins at
your first SQL statement. A
transaction ends when you issue
"EXEC SQL COMMIT" (to make
permanent any database changes
during the current transaction) or
"EXEC SQL ROLLBACK" (to undo
any changes since the current
transaction began). After the
current transaction ends with
your COMMIT or ROLLBACK
statement, the next executable
SQL statement will automatically
begin a new transaction.
programmingexamples.wikidot.com/pro-c-tutorial 9/13
8/17/2021 Pro C Tutorial - Programming Examples
Error Handling
After each executable SQL
statement, your program can find
the status of execution either by
explicit checking of SQLCA, or by
implicit checking using the
WHENEVER statement.
SQLCA
SQLCA (SQL Communications
Area) is used to detect errors and
status changes in your program.
This structure contains
components that are filled in by
Oracle at runtime after every
executable SQL statement.
#ifndef SQLCA
#define SQLCA 1
struct sqlca {
/* b4 */ long sqlcode;
struct {
/* ub2 */ unsigned
/* ub1 */ char sqle
} sqlerrm;
/* ... */
programmingexamples.wikidot.com/pro-c-tutorial 10/13
8/17/2021 Pro C Tutorial - Programming Examples
site-name .wikidot.com
WHENEVER
Share on
Statement
Explore »
SQLWARNING - sqlwarn[0]
is set because Oracle
returned a warning
SQLERROR - sqlcode is
negative because Oracle
returned an error
NOT FOUND - sqlcode is
positive because Oracle
could not find a row that
meets your WHERE
condition, or a SELECT INTO
or FETCH returned no rows
programmingexamples.wikidot.com/pro-c-tutorial 11/13
8/17/2021 Pro C Tutorial - Programming Examples
for (;;) {
printf("Give student id
scanf("%d", &id);
notfound:
/* ... */
Bibliography
1. Introduction to Pro*C
programmingexamples.wikidot.com/pro-c-tutorial 12/13
8/17/2021 Pro C Tutorial - Programming Examples
site-name
Unless otherwise stated, .wikidot.com
the content of this page isShare on
under Creative
licensed Explore »
Commons Attribution-
Share Alike 2.5 License.
Песочница
Закулисья
привет
programmingexamples.wikidot.com/pro-c-tutorial 13/13