我有以下C代码,它使用存储过程将2个字符串插入数据库:
char query[1024];
memset(query, 0, sizeof(query));
sprintf(query, "BEGIN bns_saa_message_insert (:1, :2); END;");
/* prepare statement */
if( checkerr(errhp, OCIStmtPrepare(stmthp, errhp, (text *) query,
(ub4) strlen((char *) query),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT)) == OCI_ERROR)
return -1;
/* bind input params */
if( checkerr(errhp, OCIBindByPos(stmthp, &bndhp, errhp, (ub4) 1, (dvoid *) hostNumber,
(sword) sizeof(hostNumber) - 1, SQLT_CHR, (dvoid *) 0,
(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT)) == OCI_ERROR)
return -1;
if( checkerr(errhp, OCIBindByPos(stmthp, &bndhp, errhp, (ub4) 1, (dvoid *) saaMessage,
(sword) sizeof(saaMessage) - 1, SQLT_CHR, (dvoid *) 0,
(ub2 *) 0, (ub2) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT)) == OCI_ERROR)
return -1;
//end of param binding
printf(“query:%s”,query);//这表明在我执行上述绑定时,param1和param2没有被替换
/* execute the statement */
status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0,
(CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT);
我得到的错误是:
ORA-01008:并非所有变量都绑定
上面代码中概述的printf输出:
查询:开始bns_saa_消息插入(:1,:2);结束;
问题
如何修复此错误?
编辑
我在这里看到了用C或Java回答的类似问题,但不是C
"ORA-01008: not all variables bound" error
ORA-01008: not all variables bound. They are bound
最佳答案
看起来是个小错误。对OCIBindByPos
的第二次调用应该对第四个参数使用2
而不是1
:
if( checkerr(errhp, OCIBindByPos(stmthp, &bndhp, errhp, (ub4) 2, (dvoid *) saaMessage,