Oracle 中oci 关于null 的处理方法

5 篇文章 0 订阅
4 篇文章 0 订阅

转载自:http://www.linuxidc.com/Linux/2011-02/32564p2.htm

官方文档中的描述

Input

For input host variables, the OCI application can assign the following values to an indicator variable:

Table 2-7 Input Indicator Values Input Indicator Value Action Taken by Oracle 
-1
 Oracle assigns a NULL to the column, ignoring the value of the input variable.
 
>=0
 Oracle assigns the value of the input variable to the column.
 


Output

On output, Oracle can assign the following values to an indicator variable:

Table 2-8 Output Indicator Values Output Indicator Value Meaning 
-2
 The length of the item is greater than the length of the output variable; the item has been truncated. Additionally, the original length is longer than the maximum data length that can be returned in the sb2 indicator variable.
 
-1
 The selected value is null, and the value of the output variable is unchanged.
 
 

0
 Oracle assigned an intact value to the host variable.
 
>0
 The length of the item is greater than the length of the output variable; the item has been truncated. The positive value returned in the indicator variable is the actual length before truncation.
 


Indicator Variables for Named Data Types and REFs
Indicator variables for most new (after release 8.0) datatypes function as described above. The only exception is SQLT_NTY (a named datatype). Data of type SQLT_REF uses a standard scalar indicator, just like other variable types. For data of type SQLT_NTY, the indicator variable must be a pointer to an indicator structure.

When database types are translated into C struct representations using the Object Type Translator (OTT), a null indicator structure is generated for each object type. This structure includes an atomic null indicator, plus indicators for each object attribute.

sb2 indicator ;

对于in

indicator = -1;

OCIBindByPos(stmthp,&bindhp,errhp,1, (dvoid *)&aa,4, SQLT_INT, (void*)&indicator, NULL, NULL,0,0,0))

当indicator = -1;无论绑定的是任何值,插入数据库的全为null

对于out

 初始

indicator = 0;

OCIDefineByPos(stmthp,&defhp1,errhp,1,&a,sizeof(a),SQLT_INT, (void*)&indicator,0,0,OCI_DEFAULT);

执行完成,当indicator = -1;如数据库中的值为null,则绑定的值不修改,并且不报错,否则替换成数据库中的值,

如果没传入indicator参数,而只是指定一个空指针的话,报错,说提取的列为null,

-2 和 >0时,测试发现差不多,都是当绑定值小于数据库的值时,把indicator 置为数据库中的实际长度,用处不大

综上所述主要就是indicator = -1,可以在数据库中插入null值,或判断数据库中的是否为null

参数indp是指示符缓冲区,也是一个数组,每个元素是一个sb2类型的值。一般作输入用,如果此项动态参数会被输出,则也作输出用。在输入时,元素值为-1时表示NULL值输入,大于或等于0时是普通得值输入。在输出时,和函数OCIDefineByPos()里的指示符缓冲区作用相同,其值将会在提取数据后填入。其值标志着取到的数值的特殊信息。-2表示值的长度过大,且超过sb4类型的最大值,取出的值被截断,是部分值。-1表示值为NULL,因为C/C++没有NULL这个类型,因此这是判断取出的字段值是否是NULL的唯一方法。0表示数据被完整取出。大于0的值,表示取出的值的字节大小超过定义的每个值的字节大小,取出的值被截断,返回的值是被截断前的字节的大小。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Oracle OCIOracle Call Interface)接口连接数据库、创建表和插入数据的简单示例代码。请注意,这个示例只是一个基本的样本,您可能需要根据您的具体情况进行修改。 ```c #include <stdio.h> #include <stdlib.h> #include <oci.h> int main() { OCIEnv *envhp; OCIError *errhp; OCIServer *srvhp; OCISession *usrhp; OCISvcCtx *svchp; OCIDefine *defhp; OCIStmt *stmthp; sword status; text sqlstmt[1024]; int empno; text ename[20]; int sal; // 初始化 OCI 环境 status = OCIEnvCreate(&envhp, OCI_THREADED | OCI_OBJECT, NULL, NULL, NULL, NULL, 0, NULL); if (status != OCI_SUCCESS) { printf("OCIEnvCreate failed\n"); return 1; } // 分配错误句柄 status = OCIHandleAlloc(envhp, (void **)&errhp, OCI_HTYPE_ERROR, 0, NULL); if (status != OCI_SUCCESS) { printf("OCIHandleAlloc failed for error handle\n"); return 1; } // 分配服务器句柄 status = OCIHandleAlloc(envhp, (void **)&srvhp, OCI_HTYPE_SERVER, 0, NULL); if (status != OCI_SUCCESS) { printf("OCIHandleAlloc failed for server handle\n"); return 1; } // 创建服务器上下文 status = OCIServerAttach(srvhp, errhp, (text *)"ORCL", strlen("ORCL"), 0); if (status != OCI_SUCCESS) { printf("OCIServerAttach failed\n"); return 1; } // 分配服务句柄 status = OCIHandleAlloc(envhp, (void **)&svchp, OCI_HTYPE_SVCCTX, 0, NULL); if (status != OCI_SUCCESS) { printf("OCIHandleAlloc failed for service context handle\n"); return 1; } // 设置服务上下文服务器句柄 status = OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, srvhp, 0, OCI_ATTR_SERVER, errhp); if (status != OCI_SUCCESS) { printf("OCIAttrSet failed for service context handle\n"); return 1; } // 分配用户句柄 status = OCIHandleAlloc(envhp, (void **)&usrhp, OCI_HTYPE_SESSION, 0, NULL); if (status != OCI_SUCCESS) { printf("OCIHandleAlloc failed for user handle\n"); return 1; } // 设置用户句柄的用户名和密码 status = OCIAttrSet(usrhp, OCI_HTYPE_SESSION, (void *)"scott", strlen("scott"), OCI_ATTR_USERNAME, errhp); if (status != OCI_SUCCESS) { printf("OCIAttrSet failed for user handle\n"); return 1; } status = OCIAttrSet(usrhp, OCI_HTYPE_SESSION, (void *)"tiger", strlen("tiger"), OCI_ATTR_PASSWORD, errhp); if (status != OCI_SUCCESS) { printf("OCIAttrSet failed for user handle\n"); return 1; } // 开始会话
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值