日期:2014-05-17 浏览次数:20831 次
/*************************
编译语句
gcc -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -I${ORACLE_HOME}/rdbms/public -I${ORACLE_HOME}/rdbms/demo -L${ORACLE_HOME}/lib -lclntsh test1.c
OCI重定义数据类型:
typedef unsigned char ub1;
typedef signed char sb1;
typedef unsigned short ub2;
typedef signed short sb2;
typedef unsigned int ub4;
typedef signed int sb4;
typedef ub4 duword;
typedef sb4 dsword;
typedef dsword dword;
*************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <oci.h>
/*user name and password*/
static text* username=(text *)"scott";
static text* password=(text *)"tiger";
static text* oracle=(text *)"orcl";
/* Define SQL statements to be used in program. */
static text* SQL=(text *)"insert into tab1(id, name) values (:1, :2)";
/*handle define*/
static OCIEnv *p_env; //OCI environment handle
static OCIError *p_err; //OCI error handle
static OCISvcCtx *p_svc; //OCI service context handel
static OCIServer *p_ser; //OCI server handle
static OCISession *p_usr; //OCI user session handle
static OCIStmt *p_sql; //OCI statement handle
static OCIDefine *p_dfn = (OCIDefine *)NULL; //OCI define handle
static OCIBind *p_bnd = (OCIBind *)NULL; //OCI bind handle
/*create OCI environment*/
int create_env()
{
int swResult; //Return value
if(swResult = OCIEnvCreate(&p_env,OCI_DEFAULT,NULL,NULL,NULL,NULL,0,NULL))
{
printf("environment create error!\n\n");
return -1;
}
else
{
printf("environment create success!\n\n");
return 0;
}
}
/*init handle*/
int init_handle()
{
int swResult;
if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_ser,OCI_HTYPE_SERVER,0,NULL)) //服务器句柄
{
printf("init server handle error!\n\n");
return -1;
}
else
{
printf("init server handle success!\n\n");
}
if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_err,OCI_HTYPE_ERROR,0,NULL)) //错误句柄
{
printf("init error handle error!\n\n");
return -1;
}
else
{
printf("init error handle success!\n\n");
}
if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_usr,OCI_HTYPE_SESSION,0,NULL)) //事务句柄
{
printf("init session handle error!\n\n");
return -1;
}
else
{
printf("init session handle success!\n\n");
}
if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_svc,OCI_HTYPE_SVCCTX,0,NULL)) //上下文句柄
{
printf("init service context handle error!\n\n");
return -1;
}
else
{
printf("init service context handel success!\n\n");
}
if(swResult = OCIHandleAlloc(p_env,(dvoid *)&p_sql,OCI_HTYPE_STMT,0,NULL)) //SQL语句句柄
{
printf("init statement handle error!\n\n");
return -1;
}
else
{
printf("init statement handle success!\n\n");
}
return 0;
}
/*connect server*/
int conn_server()
{
int swResult;
if(swResult = OCILogon(p_env,p_err,&p_svc,(text *)