Oracle Pro*C/C++(TM) Getting Started for Windows Platforms
Release 8.0
A53634-01

Library

Product

Contents

Index


Prev Next

3
Building Pro*C/C++ Applications

This chapter describes how to build Oracle database applications with Pro*C/C++ using the sample programs included with this release. It includes:

Building and Running Sample Programs

When you install Pro*C/C++, the installation procedure copies a set of sample programs and their corresponding files to the ORACLE_HOME\PRO80\C\SAMPLES subdirectory. Oracle recommends that you build and run these sample programs to verify that Pro*C/C++ is installed successfully and is operating correctly. You can delete the programs after you use them.

Directory   Sample Program   Sample Pro*C/C++ Project   Sampler Compiler Project   Description  

COLDEMO1  

COLDEMO1.PC  

COLDEMO1.PRE  

COLDEMO1.MAK
COLDEMO1.IDE  

Pro*C/C++ application that uses OTT. Note: Run COLDEMO1.SQL and OTT before building COLDEMO1.  

CPPDEMO1  

CPPDEMO1.PC  

CPPDEMO1.PRE  

 

Pro*C/C++ application in C++.  

CPPDEMO2  

CPPDEMO2.PC  

CPPDEMO2.PRE  

 

Pro*C/C++ application in C++.  

CPPDEMO3  

CPPDEMO3.PC  

CPPDEMO3.PRE  

 

Pro*C/C++ application in C++.  

CVDEMO  

CV_DEMO.PC
CV_DEMO.SQL  

CV_DEMO.PRE  

CV_DEMO.DSP
CV_DEMO.IDE  

Pro*C/C++ application using cursor variable with PL/SQL stored procedure. Note: Run CV_DEMO.SQL before building CV_DEMO.  

LOBDEMO1  

LOBDEMO1.PC  

LOBDEMO1.PRE  

LOBDEMO1.MAK
LOBDEMO1.IDE  

Pro*C/C++ application that uses LOB types. Note: Run COLDEMO1.SQL before building COLDEMO1.  

MLTTHRD1  

MLTTHRD1.PC
MLTTHRD1.SQL  

MLTTHRD1.PRE  

MLTTHRD1.DSP
MLTTHRD1.IDE  

Multi-thread Pro*C/C++ application.  

OBJDEMO1  

OBJDEMO1.PC  

OBJDEMO1.PRE  

OBJDEMO1.MAK
OBJDEMO1.IDE  

Pro*C/C++ application that uses OTT. Note: Run OBJDEMO1.SQL before building OBJDEMO1.  

ORACA  

ORACA.PC  

ORACA.PRE  

ORACA.DSP
ORACA.IDE  

Pro*C/C++ application using ORACA to determine various performance parameters at runtime. Note: Run ORACATST.SQL before running ORACA.  

PLSSAM  

PLSSAM.PC  

PLSSAM.PRE  

PLSSAM.DSP
PLSSAM.IDE  

Pro*C/C++ with embedded
PL/SQL blocks.  

SAMPLE  

SAMPLE.PC  

SAMPLE.PRE  

SAMPLE.DSP
SAMPLE.IDE  

Application using EXEC SQL statements.  

SAMPLE1  

SAMPLE1.PC  

SAMPLE1.PRE  

SAMPLE1.DSP
SAMPLE1.IDE  

Application using EXEC SQL statements.  

SAMPLE2  

SAMPLE2.PC  

SAMPLE2.PRE  

SAMPLE2.DSP
SAMPLE2.IDE  

Application using EXEC SQL statements.  

SAMPLE3  

SAMPLE3.PC  

SAMPLE3.PRE  

SAMPLE3.DSP
SAMPLE3.IDE  

Application using EXEC SQL statements.  

SAMPLE4  

SAMPLE4.PC  

SAMPLE4.PRE  

SAMPLE4.DSP
SAMPLE4.IDE  

Application using EXEC SQL statements.  

SAMPLE5  

SAMPLE5.PC
EXAMPBLD.SQL
EXAMPLOD.SQL  

SAMPLE5.PRE  

SAMPLE5.DSP
SAMPLE5.IDE  

Pro*C/C++ with embedded
PL/SQL blocks. NOTE: Run EXAMPBLD.SQL before running EXAMPLOD.SQL, then build SAMPLE5.  

SAMPLE6  

SAMPLE6.PC  

SAMPLE6.PRE  

SAMPLE6.DSP
SAMPLE6.IDE  

Application using EXEC SQL statements.  

SAMPLE7  

SAMPLE7.PC  

SAMPLE7.PRE  

SAMPLE7.DSP
SAMPLE7.IDE  

Application using EXEC SQL statements.  

SAMPLE8  

SAMPLE8.PC  

SAMPLE8.PRE  

SAMPLE8.DSP
SAMPLE8.IDE  

Application using EXEC SQL statements.  

SAMPLE9  

SAMPLE9.PC
CALLDEMO.SQL  

SAMPLE9.PRE  

SAMPLE9.DSP
SAMPLE9.IDE  

Pro*C/C++ application calling a PL/SQL stored procedure. NOTE: Run CALLDEMO.SQL before building SAMPLE9.  

SAMPLE10  

SAMPLE10.PC  

SAMPLE10.PRE  

SAMPLE10.DSP
SAMPLE10.IDE  

Pro*C/C++ application showing an implementation of Dynamic SQL Method 4.  

SQLVCP  

SQLVCP.PC  

SQLVCP.PRE  

SQLVCP.DSP
SQLVCP.IDE  

Pro*C/C++ application demonstrating use of sqlvcp( ) and sqlgis( ) calls.  

WINSAM  

WINSAM.PC  

WINSAM.PRE  

WINSAM.DSP
WINSAM.IDE
WINSAM.H
RESOURCE.H
WINSAM.RC
WINSAM.ICO  

GUI Pro*C/C++ application.  

Note:

The.DSP files are for Microsoft Visual C++ and .IDE files are for Borland C++.  

.DSP and .IDE Files

.DSP and .IDE files help you build application programs. They guide and control the steps necessary to precompile, compile, and link sample programs. Sample .DSP files in this release, are created with Microsoft Visual C++ Version 5.0, while sample .IDE files in this release are created with Borland C++ Version 5.0.

Sample Pro*C/C++ Programs

When built, the sample programs that Oracle provides in Pro*C/C++ produce .EXE executables. For some sample programs, you must run the SQL script(s) in the sample directories before you precompile and run the sample program. The SQL scripts set up the correct tables and data so that the sample programs run successfully.

Also, some samples require OTT (Object Type Translator) to be executed before precompilation. OTT is used to create header files with C-struct representations of Abstract Data Types (ADTs) which have been created and stored in an Oracle8 database. The sample programs are described below.

COLDEMO1.PC

Fetches census information for certain California County or Counties. This sample demonstrates a number of ways to navigate through collection-typed database columns.

CPPDEMO1.PC

Prompts the user for an employee number, then queries the EMP table for the employee's name, salary, and commission. It uses indicator variables (in an indicator struct) to determine if the commission is NULL.

CPPDEMO2.PC

Retrieves the names of all employees in a given department from the EMP table (Dynamic SQL Method 3).

CPPDEMO3.PC

An example of C++ Inheritance, this program finds all salespeople and prints their names and their total earnings (including commissions).

CV_DEMO.PC

Fetches from the EMP table, using a cursor variable. The cursor is opened in the stored PL/SQL procedure open_cur, in the EMP_DEMO_PKG package.

LOBDEMO.PC

Fetches and adds crime records to the database based on the person's social security number. This sample demonstrates the mechanisms for accessing and storing LOBs (Large OBjects) to tables and manipulating LOBs through the stored procedures available via the dbms_lob package.

MLTTHRD1.PC

This program shows how to use threading in conjunction with precompilers. The program creates as many sessions as there are threads.

Each thread executes zero or more transactions, that are specified in a transient structure called, "records". The program requires a table,

"ACCOUNTS" to be in the schema scott/tiger. The description of ACCOUNTS is:

SQL> desc accounts

Name  

Null  

Type  

---------  

--------  

-------  

ACCOUNT  

 

NUMBER (10)  

BALANCE  

 

NUMBER (12,2)  

For proper execution, the table should be filled with the accounts 10001 to 10008.

OBJDEMO1.PC

Demonstrates the use of objects. This sample manipulates the object types "person" and "address".

ORACA.PC

Demonstrates how to use the ORACA to determine various performance parameters at runtime.

PLSSAM.PC

Demonstrates the use of embedded PL/SQL blocks in a Pro*C/C++ application. This program prompts you for an employee name that already resides in a database. It then executes a PL/SQL block, which returns the results of four SELECT statements.

For example, if you enter the name SMITH, the following information is displayed:

SMITH's job is:  CLERK

Hired on:  17-DEC-80

0 people have served longer

Salary is: 800

15 people have a higher salary

Department number is: 20

6 people in the department

If you enter a name that does not reside in the database, the following message is displayed, where ename is the name you entered:

ename does not work for the company

SAMPLE.PC

Adds new employee records to the personnel database and checks database integrity. The employee numbers in the database are automatically selected using the current maximum employee number +10. The program asks you to enter the following data:

If the record has been inserted successfully, the following is printed:

ename added to department dname as employee # nnnnnn

where ename is the employee name you entered, dname is the department name, and nnnnn is the automatically generated employee number.

SAMPLE1.PC

Logs on to an Oracle database, prompts the user for an employee number, queries the database for the employee's name, salary, and commission, and displays the result. It continues until you enter a 0 (zero) as the employee number.

SAMPLE2.PC

Logs on to an Oracle database, declares and opens a cursor, fetches the names, salaries, and commissions of all salespeople, displays the results, then closes the cursor.

SAMPLE3.PC

Logs on to an Oracle database, declares and opens a cursor, fetches in batches using arrays, and prints the results using the function print_rows().

SAMPLE4.PC

Features an in-depth example of Datatype Equivalencing. After logging in, it creates a new table in the SCOTT account IMAGE, and simulates placement of bitmap images of employees. Later, when an employee number is entered, the associated bitmap is selected from the IMAGE table, and "pseudo-displayed" on the screen.

SAMPLE5.PC

Prompts the user for an account number and a debit amount. It verifies that the account number is valid and that there are sufficient funds to cover the withdrawal before it debits the account.

SAMPLE6.PC

Creates a table, inserts a row, commits the insert, and drops the table (Dynamic SQL Method 1).

SAMPLE7.PC

Inserts two rows into the EMP table and deletes them (Dynamic SQL Method 2).

SAMPLE8.PC

Retrieves the names of all employees in a given department from the EMP table (Dynamic SQL Method 3).

SAMPLE9.PC

Connects to an Oracle database using the SCOTT/TIGER account. The program declares several host arrays and calls a PL/SQL stored procedure (GET_EMPLOYEES in the CALLDEMO package) that fills the table OUT parameters. The PL/SQL procedure returns up to ASIZE values. SAMPLE9 keeps calling GET_EMPLOYEES, getting ASIZE arrays each time, and printing the values, until all rows have been retrieved. GET_EMPLOYEES sets the done_flag to indicate no more data.

SAMPLE10.PC

Connects to an Oracle database using your username and password and prompts for an SQL statement. You can enter any legal SQL statement, but you must use regular SQL syntax, not embedded SQL. Your statement is processed. If it is a query, the rows fetched are displayed (Dynamic SQL Method 4).

SQLVCP.PC

Demonstrates how you can use the sqlvcp() function to determine the actual size of a VARCHAR struct. The size is then used as an offset to increment a pointer that steps through an array of VARCHARs.

This program also demonstrates how to use the sqlgls() function to get the text of the last SQL statement executed.

WINSAM.PC

This GUI version of the SAMPLE.PC program adds new employee records to the personnel database and checks database integrity. You can enter as many employee names as you want and perform the SQL commands by selecting the appropriate buttons in the Employee Record box.

Additional Information:

Refer to "Error Handling" in the Programmer's Guide to the Oracle Pro*C/C++ Precompiler.  

Creating a Sample Program

Follow these steps to precompile, compile, and link a sample program:

1. Integrate Pro*C/C++ into the development environment. (See appendices A and B.)

2. Open a sample project file.

For example, open WINSAM.DSP for Microsoft Visual C++ or open WINSAM.IDE for Borland C++. Check the paths and filenames in the project file to make sure that they correspond to the configuration of your system. If they do not, change the paths and filenames accordingly. Your system may produce error messages if the paths to all components are not correct.

Note:

All of the projects are created with C as the default drive.  

Attention:

When using Microsoft Visual C++ Version 5.0, note that SQL*Plus, OTT, and Pro*C/C++ have been fully integrated into the Microsoft Visual C++ sample project files. There is no need to run them separately before compilation. Simply run build to create the sample program using Microsoft Visual C++; then omit the remaining steps.

For Borland C++ Version 5.0, all the samples can be precompiled using Pro*C/C++ as a tool from the tool menu in Borland C++ IDE. After precompilation is finished, exit from the Pro*C/C++ application and select Make all under Project in Borland C++ IDE.  

If Borland C++ is used, some samples require SQL scripts and OTT to be executed individually before precompilation.

3. For the samples that require SQL scripts to be executed, do the following at the command line:

  plus80 @<demo>.sql

where <demo> can be replaced by a name of any sample that has a SQL script. SQL scripts for samples can be found in its respective sample directory.

For the samples the require OTT to be executed, namely coldemo1 and objdemo1, do the following at the command line to produce .h and intype files:

ott int=<demo>.typ out=out.typ hfile=<demo>.h code=c user=scott/tiger

where <demo> is coldemo1 or objdemo1. When precompiling these two samples, be sure to set intype=out.typ, parse=full, and include directory to where <demo>.h is located.

Additional Information:

For more information on OTT, see Programmer's Guide to the Oracle Pro*C/C++ Precompiler.  

4. Select Pro*C/C++ from the Tools menu.

5. The Pro*C/C++ Application window appears with the project file (for example, WINSAM.PRE) already loaded.

6. Select Run from the Toolbar to precompile the sample file.

7. Once the precompile process is successfully completed, go back to your development environment, and create the sample application by running the build process.

Running a Sample Program

To run a sample program (in this case, WINSAM) after building it, follow these steps:

  1. From your development environment, run WINSAM.EXE.
  2. Select Connect from the Oracle menu.
  3. Enter SCOTT in the User ID box and TIGER in the Password box (or the connect string if connecting remotely). Choose OK.
  4. A dialog box appears notifying you whether the connection is successful.

  5. To enter employee data, select Employees from the Oracle menu.
  6. An Employee Record dialog box appears. You can use SQL buttons such as SELECT, FETCH, or INSERT to manipulate the sample tables.

  7. Choose Exit from the Employee Record dialog box. Then, choose Disconnect from the Oracle menu.
  8. A dialog box notifies you whether the disconnect is successful.
  9. Choose Exit to quit the program.
  10. Note:

    All sample programs are written to be run on the local database. To connect to a remote database, use the LOCAL variable in the Windows NT or Windows 95 Registry.  

Building the Demonstration Tables

To run the Pro*C/C++ sample programs, you must have a database account with the username SCOTT and the password TIGER and a database with the demonstration tables EMP and DEPT. This account is included in the seed database (named ORACLE) that is shipped with your Oracle server. If no such account exists on your database, install one before running the sample program.

Additional Information:

See your Oracle database or server documentation. If your database does not contain these tables, use the DEMOBLD.SQL script to create them. To run DEMOBLD, first use SQL*Plus or Server Manager to connect to the Oracle database.  

Dynamic Link Libraries (DLLs)

The Pro*C/C++ API calls are implemented in DLL files provided with your
Pro*C/C++ software. To use the DLLs, you must link your application with the import libraries (.LIB files) that correspond to the Pro*C/C++ DLLs. Also, you must make sure that the DLL files are installed on the workstation that is running your Pro*C/C++ application.

Applications linked with DLLs provide the following benefits over static libraries:

Microsoft provides you with three libraries: LIBC.LIB, LIBCMT.LIB, and MSVCRT.LIB. The Oracle DLLs use the MSVCRT.LIB runtime library. You must link with MSVCRT.LIB rather than the other two Microsoft libraries.

Building Multi-Threaded Programs

Build multi-threaded applications if you are planning to perform concurrent database operations.

Windows NT and Windows 95 schedules and allocates threads belonging to processes. A thread is a path of a program's execution. It consists of a kernel stack, the state of the CPU registers, a thread environment block, and a users stack. Each thread shares the resources of a process. Multi-threaded applications use the resources of a process to coordinate the activities of individual threads.

Additional Information:

For more information on how to write multi-threaded applications with Pro*C/C++, see the Programmer's Guide to the Oracle Pro*C/C++ Precompiler.  

Reentrant Functions

When building a multi-threaded application, make sure that your C/C++ code is reentrant. This means that access to static or global data must be restricted to one thread at a time. If you mix multi-threaded and non-reentrant functions, one thread may potentially modify information that is required by another thread.

The Pro*C/C++ precompiler automatically creates variables on the local stack of the process. This ensures that each thread using the Pro*C/C++ function has access to a unique set of variables and is reentrant.




Prev

Next
Oracle
Copyright © 1997 Oracle Corporation.
All Rights Reserved.

Library

Product

Contents

Index