/*

 * 使用结构体进行批量插入

 */

#include <oci.h>

#include <errno.h>

#include <string>

#include <stdio.h>

#include <iostream>

using namespace std;

 

class dept{

public:

int deptno;

char dname;

char loc;

 

dept(){deptno=0;}

 

~dept(){}

 

dept(const dept &dept){

deptno = dept.deptno;

dname = dept.dname;

loc = dept.loc;

}

 

dept& operator=(const dept &dept){

deptno = dept.deptno;

dname = dept.dname;

loc = dept.loc;

return *this;

}

};

 

typedef struct{int deptno;char dname[6];char loc[12];}dept_T;

 

int main(){

sword status = 0;

dept_T deptarry[10];

for(int i=0;i<10;i++){

deptarry[i].deptno = 60+i;

strcpy(deptarry[i].dname, "test0");

strcpy(deptarry[i].loc, "bulkinsert0");

}

 

text *sqlstmt=(text *)"insert into dept(deptno,dname,loc) values (:deptno,:dname,:loc)";

 

OCIEnv *envhp;

OCIServer *srvhp;

OCIError *errhp;

OCISession *usrhp;

OCISvcCtx *svchp;

OCIStmt *stmthp;

 

OCIBind *bndp = NULL;

OCIBind *bnd1p = NULL;

OCIBind *bnd2p = NULL;

OCIBind *bnd3p = NULL;

 

OCIEnvCreate(&envhp,OCI_THREADED,(dvoid *)0,0,0,0,(size_t)0,(dvoid **)0);

OCIHandleAlloc((dvoid *)envhp,(dvoid **)&srvhp,OCI_HTYPE_SERVER,0,(dvoid **)0);

OCIHandleAlloc((dvoid *)envhp,(dvoid **)&errhp,OCI_HTYPE_ERROR,0,(dvoid **)0);

OCIServerAttach(srvhp,errhp,(text *)"",strlen(""),OCI_DEFAULT);

OCIHandleAlloc((dvoid *)envhp,(dvoid **)&svchp,OCI_HTYPE_SVCCTX,0,(dvoid **)0);

OCIHandleAlloc((dvoid *)envhp,(dvoid **)&usrhp,OCI_HTYPE_SESSION,0,(dvoid **)0);

OCIHandleAlloc((dvoid *)envhp,(dvoid **)&stmthp,OCI_HTYPE_STMT,0,(dvoid **)0);

OCIAttrSet((dvoid *)svchp,OCI_HTYPE_SVCCTX,(dvoid *)srvhp,(ub4)0,OCI_ATTR_SERVER,errhp);

OCIAttrSet((dvoid *)usrhp,OCI_HTYPE_SESSION,(dvoid *)"scott",(ub4)strlen("scott"),OCI_ATTR_USERNAME,errhp);

OCIAttrSet((dvoid *)usrhp,OCI_HTYPE_SESSION,(dvoid *)"scott",(ub4)strlen("scott"),OCI_ATTR_PASSWORD,errhp);

OCISessionBegin(svchp,errhp,usrhp,OCI_CRED_RDBMS,OCI_DEFAULT);

OCIAttrSet((dvoid *)svchp,OCI_HTYPE_SVCCTX,(dvoid *)usrhp,(ub4)0,OCI_ATTR_SESSION,errhp);

 

OCIStmtPrepare(stmthp,errhp,(text *)sqlstmt,(ub4)strlen((char *)sqlstmt),(ub4)OCI_NTV_SYNTAX,(ub4)OCI_DEFAULT);

 

OCIBindByPos(stmthp,&bndp,errhp,1,&deptarry[0].deptno,4,SQLT_INT,0, 0, 0,0,0,OCI_DEFAULT);

OCIBindArrayOfStruct(bndp,errhp,sizeof(deptarry[0]),0,0,0);//需要为每一个绑定变量设置OCIBindArrayOfStruct

OCIBindByPos(stmthp,&bndp,errhp,2,&deptarry[0].dname,6,SQLT_CHR,0, 0, 0,0,0,OCI_DEFAULT);

OCIBindArrayOfStruct(bndp,errhp,sizeof(deptarry[0]),0,0,0);

OCIBindByPos(stmthp,&bndp,errhp,3,&deptarry[0].loc,12,SQLT_CHR,0, 0, 0,3,0,OCI_DEFAULT);

OCIBindArrayOfStruct(bndp,errhp,sizeof(deptarry[0]),0,0,0);

 

status = OCIStmtExecute(svchp,stmthp,errhp,10,0,(OCISnapshot *)0,(OCISnapshot *)0,OCI_DEFAULT);

 

system("pause");

}

分享:  DeliciousGReader鲜果豆瓣CSDN网摘
Trackback

only 1 comment untill now

  1. presentable@emphatic.jellineks” rel=”nofollow”>.…

    áëàãîäàðþ!…

Add your comment now

无觅相关文章插件