C语言与OCI一起操作oracle

2024-06-12 08:38
文章标签 oracle 语言 操作 一起 oci

本文主要是介绍C语言与OCI一起操作oracle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一,oci简介:
OCI(Oracle Call Intedace,即0racle调用层接口)是Oracle公司提供的由头文件和库函数等组成的一个访问Oracle数据库的应用程序编程接口(application programming interface API),它允许开发人员在第三代编程语言(包括C, C++, COBOL 与 FORTRAN)中通过SQL(Structure Query Language)来操纵Oracle数据库,而且OCI在一定程度上支持第三代编程语言(诸如C, C++, COBOL 与 FORTRAN)的数据类型、语法等等。OCI的显著特点是全面支持Oracle的面向对象技术,同时OCI还具有如下的一些特点:
1)高度控制应用程序的执行;
2)允许开发人员应用已熟悉的第三代程序设计语言来应用OCI;
3)可以内嵌到C代码中
4)支持动态SQL;
5)几乎所有的Oracle的开发工具都支持OCI;
6)通过回调技术(callbacks)来实现动态绑定与定义;
7)通过OCI的描述函数可以获取Oracle数据库的各种参数;
8)增强了数组在DML(data manipulation language)语言中的应用;
OCI接口支持Windows NT和Windows 95/98/2000/XP操作系统,它所支持的C语言编译器包括Borland C++和MiroSoft VisualC++等。在使用0CI开发Oralce数据库应用程序之前,应首先安装这些操作系统和C语言编译工具。在选择安装OCI开发工具包后,Oracle安装程序将0CI文件拷贝到oracle主目录内的以下子目录中:
头文件oci.h在
/home/oracle_11/app/oracle/product/11.2.0/db_1/rdbms/public/oci.h
二,简单的说一下oci的工作步骤
oci编程所需要的一些数据
typedef struct
{
OCIEnv* phOCIEnv ;//OCI环境句柄
OCIError* phOCIErr;//OCI错误句柄
OCISvcCtx* phOCISvctx;//服务上下文句柄
OCIServer* phOCIServe; /服务器上下文句柄/
OCIStmt* phOCIstmt; //语句句柄
OCISession * phSession; //会话句柄
char* DBName;//数据库服务名
char* UserName;//数据库用户名
char* Pwd;//数据库密码
}OCIHP;
2.1,分配和初始化一些句柄(如图)
这里写图片描述

2.2,分配初始化好句柄以后进行连接数据库如图
这里写图片描述
2.3,连接数据库以后就要执行一些sql语句,步骤如下
数据库连接好后可以执行SQL语句:一条SQL语句在OCI应用程序中的执行步骤一般如下:(1)准备SQL语句。(2)在SQL语句中绑定需要输入到SQL语句中的变量。(3)执行SQL语句。(4)获取SQL中的输出描述。(5)定义输出变量。(6)获取数据。具体过程及过程中调用的函数如下图所示。对于SQL中的定义语句(如CREATE,DROP)和控制语句(如GRANT,REVOKE),由于没有数据的输入输出,只需要图2中第一步和第三步即可。操作语句(如INSERT,DELETE,UPDATE)则需要执行前三步。而查询语句(如SELECT)不仅可能有数据输入,而且也有数据的输出,因此需要执行六个步骤。
这里写图片描述

三,常用函数解析可以到这里下载
http://download.csdn.net/detail/u011573853/9328969

四,案例,本人写了一个很浅显的案例,实现了增删改查操作,使用绑定参数和不绑定两种方式完成的,适合我这样的新手看,高手飘过核心代码如下
全部代码可以到此下载
http://download.csdn.net/detail/u011573853/9328997

//插入数据(不绑定参数的)
void Oci_insert(OCIHP* ph,char *sql)
{printf("sql =%s\n",sql);char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);return ;}   		printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}//绑定参数插入
void Oci_insert_bang(OCIHP* ph)
{char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;OCIBind* bhp[10];  char id[5]="11";char sname[15]="liuyupei";int age=20;char sex[]="v";char sql[]="insert into stu(id,sname,age,sex) values(:Vhid,:Vhname,:Vhage,:Vhsex)";//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);return ;}   //绑定输入参数变量/*//把id 和:Vhid绑定在一起if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[0], ph->phOCIErr, (text *) ":Vhid",strlen(":Vhid"), (ub1 *)id , strlen(id)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","1参数绑定失败");return ;}//把sname 和:Vhname绑定在一起if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[1], ph->phOCIErr, (text *) ":Vhname",strlen(":Vhname"), (ub1 *)sname , strlen(sname)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","2参数绑定失败");return ;}//把age 和:Vhage绑定在一起 if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[2], ph->phOCIErr, (text *) ":Vhage",-1, (ub1 *)&age , (sword)4, SQLT_INT, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","3参数绑定失败");return ;}//把sex 和:Vhsex绑定在一起 if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[3], ph->phOCIErr, (text *) ":Vhsex",strlen(":Vhsex"), (ub1 *)sex , strlen(sex)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","4参数绑定失败");return ;}*///第二种绑定的方法OCIBindByPos(ph->phOCIstmt, &bhp[0], ph->phOCIErr, 1,(dvoid *)id, sizeof(id), SQLT_STR,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT); OCIBindByPos(ph->phOCIstmt, &bhp[1], ph->phOCIErr, 2,(dvoid *)sname, sizeof(sname), SQLT_STR,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); OCIBindByPos(ph->phOCIstmt, &bhp[2], ph->phOCIErr, 3,(dvoid *)&age, sizeof(int), SQLT_INT,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); OCIBindByPos(ph->phOCIstmt, &bhp[3], ph->phOCIErr, 4,(dvoid *)sex, sizeof(sex), SQLT_STR, (dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}
//更新数据不绑定
void Oci_update(OCIHP* ph,char *sql)
{printf("sql =%s\n",sql);char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;}   		printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}//更新数据绑定参数
void Oci_update_bang(OCIHP* ph)
{char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;OCIBind* bhp[10];  char id[5]="11";char sname[15]="liweieieieei";
;char sql[]="update stu set sname=':Vhname' where id=':Vhid'";//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);return ;}   //绑定输入参数变量/*//把id 和:Vhid绑定在一起if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[0], ph->phOCIErr, (text *) ":Vhid",strlen(":Vhid"), (ub1 *)id , strlen(id)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","1参数绑定失败");return 0;}//把sname 和:Vhname绑定在一起if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[1], ph->phOCIErr, (text *) ":Vhname",strlen(":Vhname"), (ub1 *)sname , strlen(sname)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","2参数绑定失败");return 0;}*///第二种绑定的方法OCIBindByPos(ph->phOCIstmt, &bhp[0], ph->phOCIErr, 2,(dvoid *)id, sizeof(id), SQLT_STR,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT); OCIBindByPos(ph->phOCIstmt, &bhp[1], ph->phOCIErr, 1,(dvoid *)sname, sizeof(sname), SQLT_STR,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}//查询
void Oci_select(OCIHP* ph)
{int nRet = 0;ub4 ub4RecordNo = 1;OCIDefine * bhp[10];  char id[20];char sname[30];int age;char sex[20];char sErrorMsg[1024];sb4 sb4ErrorCode;//char sname[10] ={0};b2 sb2aIndid[30]; //指示器变量,用于取可能存在空值的字char sql[]="select id,sname,age,sex  from stu  ";nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql, (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); //准备SQL语句if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);return ;}   //获取数据长度ub2 datalen = 0;//绑定输出参数if(OCIDefineByPos(ph->phOCIstmt,&bhp[0],ph->phOCIErr, 1, (dvoid *)&id, (ub4)sizeof(id),SQLT_STR/*LBI long binary type */, &sb2aIndid[0], &datalen, NULL,   OCI_DEFAULT) !=0){//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg,sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);OCIHandleFree(ph->phOCIstmt, OCI_HTYPE_STMT);printf("%s\n","1参数绑定失败");return ;}if(OCIDefineByPos(ph->phOCIstmt,&bhp[1],ph->phOCIErr, 2,(dvoid *)&sname, (ub4)sizeof(sname),SQLT_STR/*LBI long binary type */, &sb2aIndid[1], &datalen, NULL, OCI_DEFAULT) !=0){//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*)sErrorMsg,sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);OCIHandleFree(ph->phOCIstmt, OCI_HTYPE_STMT);printf("%s\n","2参数绑定失败");return ;}if(OCIDefineByPos(ph->phOCIstmt,&bhp[2],ph->phOCIErr, 3,(dvoid *)&age, (ub4)4,SQLT_INT/*LBI long binary type */, NULL, &datalen, NULL,  OCI_DEFAULT) !=0){//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);OCIHandleFree(ph->phOCIstmt, OCI_HTYPE_STMT);printf("%s\n","3参数绑定失败");return ;}if(OCIDefineByPos(ph->phOCIstmt,&bhp[3],ph->phOCIErr, 4,(dvoid *)&sex, (ub4)sizeof(sex),SQLT_STR/*LBI long binary type */, &sb2aIndid[3], &datalen, NULL, OCI_DEFAULT) !=0){//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*)sErrorMsg,sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);OCIHandleFree(ph->phOCIstmt, OCI_HTYPE_STMT);printf("%s\n","4参数绑定失败");return ;}//获取执行语句类型ub2 stmt_type;OCIAttrGet ((dvoid *)ph->phOCIstmt, (ub4)OCI_HTYPE_STMT, (dvoid *)&stmt_type, (ub4 *)0, (ub4)OCI_ATTR_STMT_TYPE, ph->phOCIErr);printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)(stmt_type==OCI_STMT_SELECT?1:0), (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} // 利用游标提取信息int rows_fetched;do{printf("id=%s,sname=%s,age=%d,sex =%s\n",id,(sb2aIndid[1]==-1?"NULL":sname),age,sex);printf("%d\n",sb2aIndid[0]);printf("%d\n",sb2aIndid[1]);}while(OCIStmtFetch2(ph->phOCIstmt, ph->phOCIErr, 1, OCI_FETCH_NEXT, OCI_FETCH_NEXT, OCI_DEFAULT) != OCI_NO_DATA);// 获得记录条数OCIAttrGet((CONST void *)ph->phOCIstmt, OCI_HTYPE_STMT, (void *)&rows_fetched, (ub4 *)sizeof(rows_fetched),OCI_ATTR_ROW_COUNT, ph->phOCIErr);printf("总共记录数 %d\n",rows_fetched);
}//删除数据
void Oci_delete(OCIHP* ph,char *sql)
{printf("sql =%s\n",sql);char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;}   		printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}
简单的makefile://插入数据(不绑定参数的)
void Oci_insert(OCIHP* ph,char *sql)
{printf("sql =%s\n",sql);char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);return ;}   		printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}//绑定参数插入
void Oci_insert_bang(OCIHP* ph)
{char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;OCIBind* bhp[10];  char id[5]="11";char sname[15]="liuyupei";int age=20;char sex[]="v";char sql[]="insert into stu(id,sname,age,sex) values(:Vhid,:Vhname,:Vhage,:Vhsex)";//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);return ;}   //绑定输入参数变量/*//把id 和:Vhid绑定在一起if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[0], ph->phOCIErr, (text *) ":Vhid",strlen(":Vhid"), (ub1 *)id , strlen(id)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","1参数绑定失败");return ;}//把sname 和:Vhname绑定在一起if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[1], ph->phOCIErr, (text *) ":Vhname",strlen(":Vhname"), (ub1 *)sname , strlen(sname)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","2参数绑定失败");return ;}//把age 和:Vhage绑定在一起 if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[2], ph->phOCIErr, (text *) ":Vhage",-1, (ub1 *)&age , (sword)4, SQLT_INT, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","3参数绑定失败");return ;}//把sex 和:Vhsex绑定在一起 if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[3], ph->phOCIErr, (text *) ":Vhsex",strlen(":Vhsex"), (ub1 *)sex , strlen(sex)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","4参数绑定失败");return ;}*///第二种绑定的方法OCIBindByPos(ph->phOCIstmt, &bhp[0], ph->phOCIErr, 1,(dvoid *)id, sizeof(id), SQLT_STR,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT); OCIBindByPos(ph->phOCIstmt, &bhp[1], ph->phOCIErr, 2,(dvoid *)sname, sizeof(sname), SQLT_STR,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); OCIBindByPos(ph->phOCIstmt, &bhp[2], ph->phOCIErr, 3,(dvoid *)&age, sizeof(int), SQLT_INT,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); OCIBindByPos(ph->phOCIstmt, &bhp[3], ph->phOCIErr, 4,(dvoid *)sex, sizeof(sex), SQLT_STR, (dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}
//更新数据不绑定
void Oci_update(OCIHP* ph,char *sql)
{printf("sql =%s\n",sql);char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;}   		printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}//更新数据绑定参数
void Oci_update_bang(OCIHP* ph)
{char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;OCIBind* bhp[10];  char id[5]="11";char sname[15]="liweieieieei";
;char sql[]="update stu set sname=':Vhname' where id=':Vhid'";//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);return ;}   //绑定输入参数变量/*//把id 和:Vhid绑定在一起if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[0], ph->phOCIErr, (text *) ":Vhid",strlen(":Vhid"), (ub1 *)id , strlen(id)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","1参数绑定失败");return 0;}//把sname 和:Vhname绑定在一起if ((nRet= OCIBindByName(ph->phOCIstmt, &bhp[1], ph->phOCIErr, (text *) ":Vhname",strlen(":Vhname"), (ub1 *)sname , strlen(sname)+1, SQLT_STR, (void *) 0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT))!=OCI_SUCCESS){printf("%s\n","2参数绑定失败");return 0;}*///第二种绑定的方法OCIBindByPos(ph->phOCIstmt, &bhp[0], ph->phOCIErr, 2,(dvoid *)id, sizeof(id), SQLT_STR,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0,OCI_DEFAULT); OCIBindByPos(ph->phOCIstmt, &bhp[1], ph->phOCIErr, 1,(dvoid *)sname, sizeof(sname), SQLT_STR,(dvoid*)0,(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT); printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}//查询
void Oci_select(OCIHP* ph)
{int nRet = 0;ub4 ub4RecordNo = 1;OCIDefine * bhp[10];  char id[20];char sname[30];int age;char sex[20];char sErrorMsg[1024];sb4 sb4ErrorCode;//char sname[10] ={0};b2 sb2aIndid[30]; //指示器变量,用于取可能存在空值的字char sql[]="select id,sname,age,sex  from stu  ";nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql, (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); //准备SQL语句if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);return ;}   //获取数据长度ub2 datalen = 0;//绑定输出参数if(OCIDefineByPos(ph->phOCIstmt,&bhp[0],ph->phOCIErr, 1, (dvoid *)&id, (ub4)sizeof(id),SQLT_STR/*LBI long binary type */, &sb2aIndid[0], &datalen, NULL,   OCI_DEFAULT) !=0){//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg,sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);OCIHandleFree(ph->phOCIstmt, OCI_HTYPE_STMT);printf("%s\n","1参数绑定失败");return ;}if(OCIDefineByPos(ph->phOCIstmt,&bhp[1],ph->phOCIErr, 2,(dvoid *)&sname, (ub4)sizeof(sname),SQLT_STR/*LBI long binary type */, &sb2aIndid[1], &datalen, NULL, OCI_DEFAULT) !=0){//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*)sErrorMsg,sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);OCIHandleFree(ph->phOCIstmt, OCI_HTYPE_STMT);printf("%s\n","2参数绑定失败");return ;}if(OCIDefineByPos(ph->phOCIstmt,&bhp[2],ph->phOCIErr, 3,(dvoid *)&age, (ub4)4,SQLT_INT/*LBI long binary type */, NULL, &datalen, NULL,  OCI_DEFAULT) !=0){//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);OCIHandleFree(ph->phOCIstmt, OCI_HTYPE_STMT);printf("%s\n","3参数绑定失败");return ;}if(OCIDefineByPos(ph->phOCIstmt,&bhp[3],ph->phOCIErr, 4,(dvoid *)&sex, (ub4)sizeof(sex),SQLT_STR/*LBI long binary type */, &sb2aIndid[3], &datalen, NULL, OCI_DEFAULT) !=0){//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*)sErrorMsg,sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);OCIHandleFree(ph->phOCIstmt, OCI_HTYPE_STMT);printf("%s\n","4参数绑定失败");return ;}//获取执行语句类型ub2 stmt_type;OCIAttrGet ((dvoid *)ph->phOCIstmt, (ub4)OCI_HTYPE_STMT, (dvoid *)&stmt_type, (ub4 *)0, (ub4)OCI_ATTR_STMT_TYPE, ph->phOCIErr);printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)(stmt_type==OCI_STMT_SELECT?1:0), (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} // 利用游标提取信息int rows_fetched;do{printf("id=%s,sname=%s,age=%d,sex =%s\n",id,(sb2aIndid[1]==-1?"NULL":sname),age,sex);printf("%d\n",sb2aIndid[0]);printf("%d\n",sb2aIndid[1]);}while(OCIStmtFetch2(ph->phOCIstmt, ph->phOCIErr, 1, OCI_FETCH_NEXT, OCI_FETCH_NEXT, OCI_DEFAULT) != OCI_NO_DATA);// 获得记录条数OCIAttrGet((CONST void *)ph->phOCIstmt, OCI_HTYPE_STMT, (void *)&rows_fetched, (ub4 *)sizeof(rows_fetched),OCI_ATTR_ROW_COUNT, ph->phOCIErr);printf("总共记录数 %d\n",rows_fetched);
}//删除数据
void Oci_delete(OCIHP* ph,char *sql)
{printf("sql =%s\n",sql);char sErrorMsg[1024];sb4 sb4ErrorCode;ub4 ub4RecordNo = 1;int nRet = 0;//准备SQL语句nRet=	OCIStmtPrepare(ph->phOCIstmt, ph->phOCIErr, (text*)sql,  (ub4) strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT); if(nRet){printf("%s\n","准备SQL语句错误");printf("OCIStmtPrepare() error:%d\n",nRet);if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;}   		printf("%s\n","kaishi执行SQL语句");nRet = OCIStmtExecute( ph->phOCISvctx, ph->phOCIstmt, ph->phOCIErr, (ub4)1, (ub4)0, (OCISnapshot *) NULL,(OCISnapshot *) NULL, (ub4) OCI_DEFAULT); //执行SQL语句if(nRet){printf("%s\n","执行SQL语句错误");printf("OCIStmtExecute() error:%d\n",nRet);//获取错误信息if (OCIErrorGet(ph->phOCIErr, ub4RecordNo++, NULL, &sb4ErrorCode, (OraText*) sErrorMsg, sizeof(sErrorMsg), OCI_HTYPE_ERROR) == OCI_SUCCESS) printf("error msg:%s\n", sErrorMsg);return ;} printf("%s\n","执行SQL语句 OK");Oci_commit( ph);return ;
}简单的makefile:gcc myocitext.c  -o myocitext -I$ORACLE_HOME/rdbms/demo -I$ORACLE_HOME/rdbms/public -lclntsh应注意库lclntsh的位置本人也是新手,下面有不错的资料
http://www.cnblogs.com/ychellboy/archive/2010/04/16/1713884.html
http://kulong0105.blog.163.com/blog/static/174406191201162145944574/

这篇关于C语言与OCI一起操作oracle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1053722

相关文章

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

Java Stream.reduce()方法操作实际案例讲解

《JavaStream.reduce()方法操作实际案例讲解》reduce是JavaStreamAPI中的一个核心操作,用于将流中的元素组合起来产生单个结果,:本文主要介绍JavaStream.... 目录一、reduce的基本概念1. 什么是reduce操作2. reduce方法的三种形式二、reduce

MySQL表空间结构详解表空间到段页操作

《MySQL表空间结构详解表空间到段页操作》在MySQL架构和存储引擎专题中介绍了使用不同存储引擎创建表时生成的表空间数据文件,在本章节主要介绍使用InnoDB存储引擎创建表时生成的表空间数据文件,对... 目录️‍一、什么是表空间结构1.1 表空间与表空间文件的关系是什么?️‍二、用户数据在表空间中是怎么

Go语言中使用JWT进行身份验证的几种方式

《Go语言中使用JWT进行身份验证的几种方式》本文主要介绍了Go语言中使用JWT进行身份验证的几种方式,包括dgrijalva/jwt-go、golang-jwt/jwt、lestrrat-go/jw... 目录简介1. github.com/dgrijalva/jwt-go安装:使用示例:解释:2. gi

Go 语言中的 Struct Tag 的用法详解

《Go语言中的StructTag的用法详解》在Go语言中,结构体字段标签(StructTag)是一种用于给字段添加元信息(metadata)的机制,常用于序列化(如JSON、XML)、ORM映... 目录一、结构体标签的基本语法二、json:"token"的具体含义三、常见的标签格式变体四、使用示例五、使用

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Go语言使用slices包轻松实现排序功能

《Go语言使用slices包轻松实现排序功能》在Go语言开发中,对数据进行排序是常见的需求,Go1.18版本引入的slices包提供了简洁高效的排序解决方案,支持内置类型和用户自定义类型的排序操作,本... 目录一、内置类型排序:字符串与整数的应用1. 字符串切片排序2. 整数切片排序二、检查切片排序状态:

Mysql数据库中数据的操作CRUD详解

《Mysql数据库中数据的操作CRUD详解》:本文主要介绍Mysql数据库中数据的操作(CRUD),详细描述对Mysql数据库中数据的操作(CRUD),包括插入、修改、删除数据,还有查询数据,包括... 目录一、插入数据(insert)1.插入数据的语法2.注意事项二、修改数据(update)1.语法2.有

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细