Re: C++ & Oracle

From: <chiappa_at_my-deja.com>
Date: Wed, 22 Nov 2000 09:41:40 GMT
Message-ID: <8vg4ck$9rt$1_at_nnrp1.deja.com>


You will need extra software (OCI or PRO*C comes from Oracle, PRO*C is included in the Oracle Enterprise client CD). See the following example using PRO*C :

/*
 *  sample3.pc
 *  Host Arrays
 *
 *  This program connects to ORACLE, declares and opens a cursor,
 *  fetches in batches using arrays, and prints the results using
 *  the function print_rows().
 */

#include <stdio.h>
#include <string.h>

#include <stdlib.h>

#include <sqlca.h>

#define NAME_LENGTH 20
#define ARRAY_LENGTH 5

/* Another way to connect. */

char *username = "SCOTT";
char *password = "TIGER";

/* Declare a host structure tag. */

struct
{

    int emp_number[ARRAY_LENGTH];
    char emp_name[ARRAY_LENGTH][NAME_LENGTH];     float salary[ARRAY_LENGTH];
} emp_rec;

/* Declare this program's functions. */

void print_rows();              /* produces program output */
void sql_error();          /* handles unrecoverable errors */


main()
{

    int num_ret; /* number of rows returned */

/* Connect to ORACLE. */

    EXEC SQL WHENEVER SQLERROR DO sql_error("Connect error:");

    EXEC SQL CONNECT :username IDENTIFIED BY :password;     printf("\nConnected to ORACLE as user: %s\n", username);

    EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error:");
/* Declare a cursor for the FETCH. */

    EXEC SQL DECLARE c1 CURSOR FOR

        SELECT empno, ename, sal FROM emp;

    EXEC SQL OPEN c1;

/* Initialize the number of rows. */

    num_ret = 0;

/* Array fetch loop - ends when NOT FOUND becomes true. */

    EXEC SQL WHENEVER NOT FOUND DO break;

    for (;;)
    {

        EXEC SQL FETCH c1 INTO :emp_rec;
/* Print however many rows were returned. */

        print_rows(sqlca.sqlerrd[2] - num_ret);
        num_ret = sqlca.sqlerrd[2];        /* Reset the number. */
    }

/* Print remaining rows from last fetch, if any. */

    if ((sqlca.sqlerrd[2] - num_ret) > 0)

        print_rows(sqlca.sqlerrd[2] - num_ret);

    EXEC SQL CLOSE c1;
    printf("\nAu revoir.\n\n\n");

/* Disconnect from the database. */

    EXEC SQL COMMIT WORK RELEASE;
    exit(0);
}

void
print_rows(n)
int n;
{

    int i;

    printf("\nNumber   Employee         Salary");
    printf("\n------   --------         ------\n");

    for (i = 0; i < n; i++)
        printf("%-9d%-15.15s%9.2f\n", emp_rec.emp_number[i],
               emp_rec.emp_name[i], emp_rec.salary[i]);

}
void
sql_error(msg)
char *msg;
{

    EXEC SQL WHENEVER SQLERROR CONTINUE;     printf("\n%s", msg);
    printf("\n% .70s \n", sqlca.sqlerrm.sqlerrmc);

    EXEC SQL ROLLBACK WORK RELEASE;
    exit(1);
}

In article <8vbjhh$a5f$1_at_eng-ser1.erg.cuhk.edu.hk>,   "Brittle" <khtsoi_at_cse.cuhk.edu.hk> wrote:

> Hi,
>
> Can anyone please give a piece of live code of connecting to
> Oracle 8i with C++ under unix/linux environment? Only a very
> simple connect/select/close example is enough. Thanks in advance.
>
> ---- Brittle
>
>


Sent via Deja.com http://www.deja.com/
Before you buy. Received on Wed Nov 22 2000 - 10:41:40 CET

Original text of this message