Friday, March 12, 2010

Connect to DB

The pro*c/c++ tool from oracle enables users to develop database driven application using C and C++.
The first step in writing a pro*c/c++ program is connecting to the database.
Below is the source code and compilation instructions for doing that.

FileName:-connect.pc
#include
#include
#include
#include
#include
#include
#include
EXEC SQL DECLARE MD_DEV DATABASE;
void sql_error()
{
size_t buff_len,msg_len;
char err_msg[250];
buff_len=sizeof(err_msg);
sqlglm(err_msg,&buff_len,&msg_len);
printf("SQL ERROR %s\n",err_msg);
exit(0);
}

int main()
{
EXEC SQL BEGIN DECLARE SECTION;
varchar usrname[20];
varchar password[20];
varchar loc[20];
EXEC SQL END DECLARE SECTION;
char sys_date[20];
memset(sys_date,' ',20);
EXEC SQL WHENEVER SQLERROR DO sql_error();

strcpy((char *) usrname.arr,"pbatch_md_int");
usrname.len=strlen( (char*)usrname.arr);
strcpy( (char *) password.arr,"pbatch_md_int");
password.len=strlen( (char*)password.arr);
strcpy( (char *) loc.arr,"oltpdev");
loc.len=strlen( (char*)loc.arr);

EXEC SQL CONNECT :usrname IDENTIFIED BY :password AT MD_DEV USING :loc;
EXEC SQL at MD_DEV select to_char(sysdate,'yyyy-mm-dd') into :sys_date from dual;
printf("SYSDATE is %.10s\n",sys_date);
return 0;
}

Compilation :
step1)
proc MODE=ORACLE SQLCHECK=SEMANTICS CHAR_MAP=VARCHAR2 CLOSE_ON_COMMIT=NO AUTO_CONNECT=NO DEFINE=UNIX INCLUDE=/usr/lib/gcc/x86_64-redhat-linux/4.1.2/include INCLUDE=/app/vector/include INCLUDE=/app/oracle/product/10.2.0/db_1/rdbms/public INCLUDE=/app/oracle/product/10.2.0/db_1/lib INCLUDE=/usr/include/linux RELEASE_CURSOR=NO HOLD_CURSOR=YES THREADS=NO PARSE=FULL method2.pc
step2)
gcc -g -I/app/oracle/product/10.2.0/db_1/precomp/public -L/app/oracle/product/10.2.0/db_1/lib -lclntsh method2.c

No comments:

Post a Comment