c oracle 框架

2023年 8月 3日 43.9k 0

在软件开发的过程中,使用框架可以大大加快开发进度和减轻开发者的工作量。其中,C Oracle框架应该是大家非常熟悉并经常使用的一种框架。

C Oracle框架的主要作用是在C语言中对Oracle数据库进行操作,让开发者能够更加方便地进行数据库相关的开发。下面我们来举几个例子来说明C Oracle框架的具体用途。

/*连接数据库*/
#include
#include
#include
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCIStmt *stmthp;
OCISession *usrhp;
OCISvcCtx *svchp;
OCIParam *parmp;
int main(){
OCIInitialize((ub4) OCI_OBJECT,&envhp,(dvoid *)0,(dvoid * (*)(dvoid *,size_t)) 0,(dvoid * (*)(dvoid *, dvoid *, size_t))0,(void (*)(dvoid *, dvoid *)) 0 );
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR, 0, (dvoid **) 0);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER, 0, (dvoid **) 0);
OCIServerAttach (srvhp, errhp, (text *) "ORCL", 4, OCI_DEFAULT);
OCIHandleAlloc ((dvoid *) envhp, (dvoid **) &svchp, (ub4) OCI_HTYPE_SVCCTX, 0, (dvoid **) 0);
OCIAttrSet ((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) srvhp, (ub4) 0, OCI_ATTR_SERVER, errhp);
OCIHandleAlloc((dvoid *) envhp, (dvoid **) &usrhp, OCI_HTYPE_SESSION, 0, (dvoid **) 0);
OCIAttrSet((dvoid *) usrhp, (ub4) OCI_HTYPE_SESSION, (dvoid *)"SCOTT", (ub4)strlen("SCOTT"), OCI_ATTR_USERNAME, errhp);
OCIAttrSet((dvoid *) usrhp, (ub4) OCI_HTYPE_SESSION, (dvoid *)"TIGER", (ub4)strlen("TIGER"), OCI_ATTR_PASSWORD, errhp);
OCISessionBegin(svchp, errhp, usrhp, OCI_CRED_RDBMS, OCI_DEFAULT);
OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) usrhp, (ub4) 0, OCI_ATTR_SESSION, errhp);
}

以上是连接数据库的代码示例。首先需要初始化环境,然后根据自己需要分配不同的Handle,最后用OCISessionBegin函数进行登录验证,即可连接到Oracle数据库。

/*创建表*/
#include
#include
#include
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCIStmt *stmthp;
OCISession *usrhp;
OCISvcCtx *svchp;
OCIParam *parmp;
int main(){
/*省略连接数据库代码*/
int ret;
char *stmt = "CREATE TABLE EMPLOYEE (EMPNO NUMBER(4) NOT NULL,ENAME VARCHAR2(10),DEPTNO NUMBER(2))";
ret = OCIStmtPrepare(stmthp, errhp, stmt, strlen(stmt), OCI_NTV_SYNTAX, OCI_DEFAULT);
if(ret != OCI_SUCCESS){
printf("OCIStmtPrepare failed.");
return -1;
}
ret = OCIStmtExecute(svchp, stmthp, errhp, OCI_DEFAULT, 0, NULL, NULL, OCI_COMMIT_ON_SUCCESS);
if(ret != OCI_SUCCESS){
printf("OCIStmtExecute failed.");
return -1;
}
printf("Table EMPLOYEE created.\n");
}

以上是创建表的代码示例。需要用OCIStmtPrepare函数准备SQL语句,然后用OCIStmtExecute函数在数据库中执行,即可在Oracle数据库中创建一张名为EMPLOYEE的表。

/*查询表*/
#include
#include
#include
OCIEnv *envhp;
OCIError *errhp;
OCIServer *srvhp;
OCIStmt *stmthp;
OCISession *usrhp;
OCISvcCtx *svchp;
OCIParam *parmp;
int main(){
/*省略连接数据库代码*/
int ret;
char *stmt = "SELECT * FROM EMPLOYEE";
ret = OCIStmtPrepare(stmthp, errhp, stmt, strlen(stmt), OCI_NTV_SYNTAX, OCI_DEFAULT);
if(ret != OCI_SUCCESS){
printf("OCIStmtPrepare failed.");
return -1;
}
ret = OCIStmtExecute(svchp, stmthp, errhp, OCI_DEFAULT, 0, NULL, NULL, OCI_COMMIT_ON_SUCCESS);
if(ret != OCI_SUCCESS){
printf("OCIStmtExecute failed.");
return -1;
}
OCIParam *colhp;
ub4 icol;
ub2 col_type;
text col_name[1024];
ub2 name_len;
sb4 col_width;
ub4 uid;
ret = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
while(ret == OCI_SUCCESS){
OCIParamGet (stmthp, OCI_HTYPE_STMT, errhp, (void **)&colhp, icol);
OCIAttrGet(colhp, OCI_DTYPE_PARAM, (void *)&col_name, (ub4 *)&name_len, OCI_ATTR_NAME, errhp);
OCIAttrGet(colhp, OCI_DTYPE_PARAM, (void *)&col_type, NULL, OCI_ATTR_DATA_TYPE, errhp);
OCIAttrGet(colhp, OCI_DTYPE_PARAM, (void *)&col_width, NULL, OCI_ATTR_DATA_SIZE, errhp);
printf("%.*s, type=%d, width=%d\n",name_len,col_name,col_type,col_width);
ret = OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
}
}

以上是查询表的代码示例。需要用OCIStmtPrepare函数准备SQL语句,然后用OCIStmtExecute函数在数据库中执行。用OCIParamGet获取每个字段的详细信息,并输出结果。

综上所述,C Oracle框架可以帮助开发者更好更快地进行Oracle数据库相关开发。通过以上示例代码,相信大家已经对C Oracle框架有了一个系统的认识。

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论