服务器 频道

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));

  9.执行SQL语句

  sword OCIStmtExecute (
  OCISvcCtx           *svchp,  //服务环境句柄
                        OCIStmt             *stmtp,  //语句句柄
                        OCIError            *errhp,
                        ub4                 iters, // **
                        ub4                 rowoff, //**
                        CONST OCISnapshot   *snap_in,
                        OCISnapshot         *snap_out,
                        ub4                 mode //**
  );
  **注:
  1. iters:对于select语句,它说明一次执行读取到buffer中的记录行数,如果不能确定select语句所返回的行数,可将iters设置为0,而对于其他的语句,iters表示这些语句的执行次数,此时iters不能为0。
  2. rowoff:在多行执行时,该参数表示从所结合的数据变量中的第几条记录开始执行(即记录偏移量)。
  3. mode:=OCI_DEFAULT:default模式
  =OCI_DESCRIBE_ONLY:描述模式,只返回选择列表的描述信息,而不执行语句
  =OCI_COMMIT_ON_SUCCESS:自动提交模式,当执行成功后,自动提交。
  =OCI_EXACT_FETCH:精确提取模式。
  =OCI_BATCH_ERRORS:批错误执行模式:用于执行数组方式的操作,在此模式下,批量insert ,update,delete时,执行过程中任何一条记录错误不会导致整个insert ,update,delete失败,系统自动会收集错误信息,而在非批错误方式下,其中的任何一条记录错误,将会导致整个操作失败。
  Eg:
  执行一次
  swResult = OCIStmtExecute(svchp, stmtp,  errhp;,
                       1, 0, NULL, NULL, OCI_DEFAULT);
  批量执行100次:
  swResult = OCIStmtExecute(svchp, stmtp,  errhp;,
                       100, 0, NULL, NULL, OCI_DEFAULT);

  10.定义输出变量
   
  OCIDefineArrayOfStruct()   Set additional attributes for static array define  
  OCIDefineByPos()   Define an output variable association  
  OCIDefineDynamic()   Sets additional attributes for define in OCI_DYNAMIC_FETCH mode  
  OCIDefineObject()   Set additional attributes for define of named data type  

  sword OCIDefineByPos (
  OCIStmt     *stmtp, //语句句柄
                        OCIDefine   **defnpp,//定义句柄—用于数组变量
                        OCIError    *errhp,
                        ub4         position,//位置序号(从1 开始)
                        dvoid       *valuep, //输出的变量名
                        sb4         value_sz, //变量长度
                        ub2         dty,  //数据类型
                        dvoid       *indp, //指示器变量/指示器变量数组,如果此字段可能存在空值,则要指示器变量,否则单条处理时为NULL
                        ub2         *rlenp, //提取的数据长度
                        ub2         *rcodep, //列级返回码数组指针
  ub4         mode //OCI_DEFAULT
  );

  
  sword OCIDefineArrayOfStruct (
  OCIDefine   *defnp,//由OCIDefineByPos定义的句柄
                  OCIError    *errhp,
  ub4         pvskip, //下一列跳过的字节数,一般就是结构的大小
                  ub4         indskip,//下一个指示器或结构跳过的字节数,=0
                  ub4         rlskip, //下一个实际值跳过的字节数,=0
                  ub4         rcskip //下一个列列级返回值跳过的字节数,=0
  );

  sword OCIDefineDynamic (
  OCIDefine   *defnp,
                          OCIError    *errhp,
                          dvoid       *octxp,
                          OCICallbackDefine       (ocbfp)(/*_
                                   dvoid          *octxp,
                                   OCIDefine      *defnp,
                                   ub4            iter,
                                   dvoid          **bufpp,
                                   ub4            **alenpp,
                                   ub1            *piecep,
                                   dvoid          **indpp,
                                   ub2            **rcodep _*/)  );

  sword OCIDefineObject ( OCIDefine       *defnp,
                         OCIError        *errhp,
                         CONST OCIType   *type,
                         dvoid           **pgvpp,
                         ub4             *pvszsp,
                         dvoid           **indpp,
                         ub4             *indszp );

  eg:

  单条查询
  sql: select username,age from student  where username=:p1;
  如果此字段有可能有空值,则
  hDefine = NULL;
  swResult = OCIDefineByPos(stmtp &hDefine, errhp, 1, tstd.username, sizeof(tstd.username), SQLT_CHR, & sb2aInd[0], NULL, NULL, OCI_DEFAULT);
  如果此字段没有空值,则
  hDefine = NULL;
  swResult = OCIDefineByPos(stmtp &hDefine, errhp, 1, tstd.username, sizeof(tstd.username), SQLT_CHR, NULL, NULL, NULL, OCI_DEFAULT);

   批量查询
  select username,age from student  where age>30;
  hDefine = NULL;
  swResult = OCIDefineByPos(stmtp, &hDefine, errhp, 1, &tstd[0].username,
  sizeof(tstd[0].usenmae), SQLT_CHR, NULL, NULL, NULL, OCI_DEFAULT);

  swResult = OCIDefineArrayOfStruct(hDefine, errhp, sizeof(tstd[0]), 0, 0, 0);

0
相关文章