Language/C, Pro*C

Pro*C 환경설정 및 Database 접속 테스트

OIZTLOMO 2009. 4. 7. 21:16

 Prerequisite:

1. You must have Oracle software installed on the HP-UX.

2. You must have PRO C plus installed on the HP-UX.

 

Steps:

1. setup your environment variables.

export ORACLE_BASE=<Value here>

export ORACLE_HOME=<Value here>

export TNS_ADMIN=<Value here>

export PATH=$ORACLE_HOME/bin:$PATH

export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH

 

2. set PRO C configuration

Edit $ORACLE_HOME/precomp/admin/pcscfg.cfg as following:

sys_include=(/usr/include)

include=(${ORACLE_HOME}/precomp/public)

include=(${ORACLE_HOME}/precomp/hdrs)

include=(${ORACLE_HOME}/tpcc2x_2/src)

include=(${ORACLE_HOME}/precomp/include)

include=(${ORACLE_HOME}/oracore/include)

include=(${ORACLE_HOME}/oracore/public)

include=(${ORACLE_HOME}/rdbms/include)

include=(${ORACLE_HOME}/rdbms/public)

include=(${ORACLE_HOME}/rdbms/demo)

include=(${ORACLE_HOME}/nlsrtl/include)

include=(${ORACLE_HOME}/nlsrtl/public)

include=(${ORACLE_HOME}/network_src/include)

include=(${ORACLE_HOME}/network_src/public)

include=(${ORACLE_HOME}/network/include)

include=(${ORACLE_HOME}/network/public)

include=(${ORACLE_HOME}/plsql/public)

ltype=short

define=ORASTDARG

 

3. Try to create a new embedded SQL/C file as following

***test.pc***

#include <stdio.h>

EXEC SQL INCLUDE SQLCA;

 

int main() {

  /*Start Session*/

  EXEC SQL BEGIN DECLARE SECTION;

  VARCHAR user[20],pass[20],tnsname[20];

  char name[20];

  int  id;

  EXEC SQL END DECLARE SECTION;

  int i=0;

  /*change the username here*/

  strcpy(user.arr,"username");

  user.len=(unsigned short)strlen((char *)user.arr);

  /*change the password here*/

  strcpy(pass.arr,"password");

  pass.len=(unsigned short)strlen((char *)pass.arr);

  /*change the tnsname entry here*/

  strcpy(tnsname.arr,"tnsname");

  tnsname.len=(unsigned short)strlen((char *)tnsname.arr);

 

  /*Connect Database*/

  EXEC SQL WHENEVER SQLERROR GOTO error_msg;

  EXEC SQL CONNECT :user IDENTIFIED BY :pass using :tnsname;

    printf("Connect successful\n");

    printf("Starting to get data from emp\n");

  EXEC SQL declare emp_cursor cursor for select id, name from emp;

  EXEC SQL open emp_cursor;

  EXEC SQL WHENEVER NOT FOUND DO break;

 

    while(1) {

      EXEC SQL fetch emp_cursor into :id, :name;

      printf("ID is %d || Name is %s\n", id, name);

    }

    EXEC SQL close emp_cursor;

    EXEC SQL commit work release;

    return 0;

  error_msg:

  printf("Connect failed\n");

  return -1; 

}

 

4. compile the embedded file into a c file.

$ proc iname=test.pc oname=test.c

 

5. compile the c file into executable file

    If you are on 32 bits server, as following:

$ /opt/ansic/bin/cc -I${ORACLE_HOME}/precomp/public -L${ORACLE_HOME}/lib -lclntsh -o test test.c

If you are on 64 bits server, as following:

/opt/ansic/bin/cc +DA2.0W -I${ORACLE_HOME}/precomp/public -L${ORACLE_HOME}/lib -lclntsh -o test test.c

      

 

6. run the executable file. And check the output.