Database

[ORACLE]Pro*C 예제

mmresult 2018. 5. 4. 09:58

makefile

CC=cc

PROC=proc

CFLAGS=+DD64

LIB=-L$(ORACLE_HOME)/lib -Iclntsh

INC=-I$(ORACLE_HOME)/precomp/public

PROCFLAGS=parse=full

test: test.pc

$(PROC) $(PROCFLAGS) iname=$(@:=.pc)

$(CC) $(CFLAGS) -o $@ $@.c $(LIB) $(INC)


test.pc

#include <stdio.h>

#include <string.h>


int main() {

/* Start Session */

EXEC SQL BEGIN DECLARE SECTION;

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

char name[20];

int id;

int i = 0;

const char* uid = "SCOTT";

const char* pwd = "TIGER";

const char* tns = "OOCN";

EXEC SQL END DECLARE SECTION;


/* change the username here */

strcpy((char*)user.arr, uid);

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


/* change the password here */

strcpy((char*)pass.arr, pwd);

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


/* change the tnsname entry here */

strcpy((char*)tnsname.arr, tns);

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


/* Connect Database */

EXEC SQL WHENEVER SQLERROR GOTO err_msg;

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


printf("Connect successful\n");


EXEC SQL declare emp_cursor cursor for select empno, ename 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;

}