Oracle常用的OCI函数之四
【IT168 服务器学院】8. 绑定输入参数
OCIBindArrayOfStruct() Set skip parameters for static array bind ,数组绑定,一般用于批量操作
OCIBindByName() Bind by name 按名绑定
OCIBindByPos() Bind by position 按位置绑定,建议一般按此方式绑定
OCIBindDynamic() Sets additional attributes after bind with OCI_DATA_AT_EXEC mode
OCIBindObject() Set additional attributes for bind of named data type
注:
OCIBindArrayOfStruct必须先用OCIBindByPos初始化,然后在OCIBindArrayOfStruct中定义每个参数所跳过的字节数。
如:
存储方式:
第一条记录第二条记录 N
SkipPara(实际就是结构体长度,即本次所有列的长度和)
sword OCIBindByName (
OCIStmt *stmtp, //语句句柄
OCIBind **bindpp,//结合句柄,=NULL
OCIError *errhp,
CONST text *placeholder,//占位符名称
sb4 placeh_len, //占位符长度
dvoid *valuep, //绑定的变量名
sb4 value_sz, //绑定的变量名长度
ub2 dty, //绑定的类型
dvoid *indp, //指示符变量指针(sb2类型),单条绑定时为NULL,
ub2 *alenp, //说明执行前后被结合的数组变量中各元素数据实际的长度,单条绑定时为NULL
ub2 *rcodep,//列级返回码数据指针,单条绑定时为NULL
ub4 maxarr_len, //最多的记录数,如果是单条绑定,则为0
ub4 *curelep, //实际的记录数,单条绑定则为NULL
ub4 mode //=OCI_DEFAULT
);
sword OCIBindByPos ( OCIStmt *stmtp,
OCIBind **bindpp,
OCIError *errhp,
ub4 position,// 绑定的位置
dvoid *valuep,
sb4 value_sz,
ub2 dty,
dvoid *indp,
ub2 *alenp,
ub2 *rcodep,
ub4 maxarr_len,
ub4 *curelep,
ub4 mode );
sword OCIBindArrayOfStruct (
OCIBind *bindp,//绑定的结构句柄,由OCIBindByPos定义
OCIError *errhp,
ub4 pvskip, //下一列跳过的字节数**
ub4 indskip,//下一个指示器或数组跳过的字节数
ub4 alskip, //下一个实际值跳过的字节数
ub4 rcskip //下一个列级返回值跳过的字节数
);
例:
sword swResult;
OCIBind* hBind;
Ub4 rec_num;
Sql: insert into student values (:p1,:p2)
单条绑定:
hBind = NULL;
swResult = OCIBindByPos(stmtp &hBind, errhp,1,ststd.tname,
sizeof(ststd.tname), SQLT_CHR, NULL,
NULL,NULL,0, NULL, OCI_DEFAULT);
批量取数据,一次取100条
Sql: select username,age from student where username=:p1 and age=:p2
hBind = NULL;
swResult = OCIBindByPos(stmtp &hBind, errhp,1,tstd[0].tname,
sizeof(tstd[0].tname), SQLT_CHR, &tstdInd.sb2_usernmae[0],
&tstdLen.ub2_username[0],&tstdRet.ub2_username[0],100, &rec_num, OCI_DEFAULT);
swResult = OCIBindArrayOfStruct(hBind, errhp,sizeof(tstd [0]), sizeof(sb2), sizeof(ub2), sizeof(ub2));