Interacting with Oracle Database using C language – Part 1 : PRO*C

In a previous post we installed Oracle client (64 bits) in order to interact with Oracle database, let’s do so using C language:
We have several options to interact with Oracle database using C. In this post we will focus on PRO*C.

Pro*C :

ProC (also known as ProC/C++) is an embedded SQL programming language used by Oracle database. ProC uses either C or C++ as its host language. During compilation, the embedded SQL statements are interpreted by a precompiler and replaced by C or C++ function calls to their respective SQL library. The output from the ProC precompiler is standard C or C++ code that is then compiled by any one of several C or C++ compilers into an executable.

Verify that proc precompiler is installed :

Then create this file /usr/lib/oracle/11.2/client64/precomp/admin/pcscfg.cfg with this contents :

Then we create a simple program hello_oracle_proc.pc to connect to our database (hr schema) and perform some requests :

Here some explanations about this code :

All requests in PRO*C begins with : EXEC SQL followed by the request

The indicator variables are not mandatory, we can for example do this :

and remove all the code containing “indicator variables” inside our program

To pass a variable inside a request we use this syntax : :variable

The VARCHAR data structure is defined like this :

So to go from VARCHAR variable to (char *), you do this :

And to go from (char *) to VARCHAR, you do this :

Now we can do the precompilation step like this :

This will generate a C file hellooracleproc.c, that we can compile normally like this :

You may get a warning like this :

To hide this warning, you can use the -Wno-unused-variable gcc compiler option :

Then run the program like this :

You will got something like this :

In a next post we’ll continue our exploration on how to interact with Oracle database using C language.