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

相关文章

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-