OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); 
  char sql[255]; 
  sprintf(sql,"%s","insert into scott.emp(empno) values(:9002)"); 
  if (OCIStmtPrepare(stmthpp, errhpp, (text *)sql, (ub4)strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT) != OCI_SUCCESS) 
  { 
              cout < < "Create prepare error!" < < sql < < endl; 
              exit(1); 
    } 
      cout < < "Create prepare success!" < < endl; 
  int i; 
  OCIBind *hbind1 = NULL; 
  // OCIBind *hbind2 = NULL; 
    errhpp=NULL; 
  i=OCIHandleAlloc((dvoid *)envhpp,(dvoid **)&errhpp,OCI_HTYPE_ERROR,(size_t)0, (dvoid **)0); 
  
  
  ub2 stmt_type; 
  i=OCIBindByPos(stmthpp,&hbind1,errhpp,1,(dvoid *)&gather.id ,  sizeof(int),SQLT_INT,NULL,NULL,NULL,0,NULL,OCI_DEFAULT); 
  // i=OCIBindByPos(stmthpp,&hbind2,errhpp,2,(dvoid *)&gather.name,sizeof(gather.name),SQLT_CHR,NULL,NULL,NULL,0,NULL,OCI_DEFAULT); 
//OCIDefineByPos(stmthpp, &bhp1, errhpp, 1, (dvoid *)&gather. id, sizeof(int), SQLT_INT,NULL, &datalen, NULL, OCI_DEFAULT); 
  i=OCIAttrGet ((dvoid *)stmthpp, (ub4)OCI_HTYPE_STMT, (dvoid *)&stmt_type, (ub4 *)0, (ub4)OCI_ATTR_STMT_TYPE, errhpp); 
  // 执行 SQL 语句 
  i=OCIStmtExecute(svcctx, stmthpp, errhpp, (ub4)(stmt_type==OCI_STMT_SELECT?1:0), (ub4)0, (OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT); 
  if( ( i)!=OCI_SUCCESS) 
  { 
              int errcno; 
              char errbuf[512]={'\0'}; 
              sb4 errcode; 
            // 返回一个错误指针和一个 OCI 错误代码 
          OCIErrorGet((dvoid *)errhpp, (ub4)1, (text *)NULL, &errcode, (ub1 *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); 
              errcno = errcode; 
              cout < < "Oracle execute failed:" < < errbuf < < endl; 
              OCIHandleFree((dvoid *)envhpp,OCI_HTYPE_ENV); 
              OCIHandleFree((dvoid *)svcctx,OCI_HTYPE_SVCCTX); 
              //OCIHandleFree((dvoid *)stmthpp,OCI_HTYPE_STMT); 
  OCIHandleFree((dvoid *)errhpp,OCI_HTYPE_ERROR); 
              exit(1); 
  } 
  cout < < "Oracle execute success!" < < endl; 
  
  OCILogoff(svcctx, errhpp); 
  OCIServerDetach(srvhpp, errhpp,  OCI_DEFAULT); 
  OCIHandleFree((dvoid *)  stmthpp, OCI_HTYPE_STMT); 
  OCIHandleFree((dvoid *)  svcctx,  OCI_HTYPE_SVCCTX); 
  OCIHandleFree((dvoid *)  srvhpp, OCI_HTYPE_SERVER);  
  OCIHandleFree((dvoid *)  errhpp,  OCI_HTYPE_ERROR); 
  return 0; 
这是我写的关于数据库执行sql语句的程序,可编译能通过,就是跑的时候出现问题说是ora-24333  零迭代器的问题,请牛人榜我看一看程序,到底是哪里有问题,或者是还有什么地方没写完整

解决方案 »

  1.   

    sprintf(sql,"%s","insert into scott.emp(empno) values(:9002)"); values(:9002)括号里的值从哪里赋的
    我是看不出来
    :9002是不允许的
      

  2.   

    呵呵,谢谢楼上的回答,
    不过我自己已经找出答案了,我不用占位符也可以实现insert的插入,问题出错的地方是在
     i=OCIStmtExecute(svcctx, stmthpp, errhpp, (ub4)(stmt_type==OCI_STMT_SELECT?1:0), (ub4)0, (OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT); 这条语句上,stmt_type==OCI_STMT_SELECT?1:0),  因为当不是select的时候,这个参数应该是(ub4)1, 当我改成这样的时候编译就顺利通过了,而且还实现了delete update create,drop  语句的功能的功能,呵呵。
    问题已经找到,但是我现在还是不知道占位符是干什么用的,具体用法是干啥?有哪位牛人帮忙解答一下
      

  3.   

    占位符??看来看去有点像Java的PreparedStatement的?,它表示的是一个输入或输出参数,需要在下面来指定其值的。OCI不懂,但是java的可以给个例子,看看是不是差不多?public static void readFileWriteBack(String fileName, Connection con)
    throws Exception {
    String s = "";
    File f = new File(fileName);
    if (!f.exists()) {
    throw new Exception("file doesn't exist....");
    }
    BufferedReader br = null;
    PreparedStatement pstmt = null;
    try {
    br = new BufferedReader(new InputStreamReader(
    new FileInputStream(f)));
    String sql = "insert into tbl_report(a,b,c,d)"
    + "values(?,?,?,'2009-12-07')";
    pstmt = con.prepareStatement(sql);
    // 循环外部准备好prepareStatement;
    while ((s = br.readLine()) != null) {
    if (s.indexOf("合计") > 0) {
    continue;
    }
    String[] c = s.split("\t");
    printArray(c);
    if (c.length == 3) {
    // 加入批量参数
    pstmt.setString(1, c[0]);
    pstmt.setString(2, c[1]);
    pstmt.setString(3, c[2]);
    pstmt.addBatch();
    }
    }
    // 一次执行。
    pstmt.executeBatch();
    System.out.println("file write back finished");
    } catch (Exception e) {
    throw e;
    } finally {
    try {
    if (pstmt != null) {
    pstmt.close();
    }
    br.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  4.   

    感谢crazylaa 的热心帮助,我想我现在应该结帖了