0

我有以下 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("查询: %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 输出:

查询:BEGIN bns_saa_message_insert (:1, :2); 结尾;

问题
如何解决此错误?

编辑
我在这里看到了用 C# 或 Java 回答的类似问题,但不是 C
“ORA-01008:并非所有变量都绑定”错误
ORA-01008:并非所有变量都绑定。他们被束缚

4

1 回答 1

4

这看起来像一个小错误。您的第二次调用OCIBindByPos应该使用2而不是1第四个参数:

if( checkerr(errhp, OCIBindByPos(stmthp, &bndhp, errhp, (ub4) 2, (dvoid *) saaMessage,
于 2012-02-08T18:38:08.163 回答