使用C ++和OCCI在CLOB中写入

时间:2014-02-20 10:26:39

标签: c++ oracle oracle11g occi

我正在开发一个填充Oracle(11g)数据库的C ++应用程序。此数据库具有包含以下签名的包:

FUNCTION SAVE_CONF
(I_USER IN VARCHAR2
,I_APP IN VARCHAR2
,I_CONTEXT IN VARCHAR2
,O_CFG OUT SYS_REFCURSOR  --CLOB
,O_ERROR_MSG OUT VARCHAR2
)
RETURN NUMBER;

但是,我无法在我的应用程序中访问O_CFG refcursor。这就是我定义SQL语句的方式。

OraStmt->setSQL("BEGIN :1 := SCHM.PCK.SAVE_CONF(:2,:3,:4,:5,:6); END;");
OraStmt->registerOutParam(1, OCCIINT);
OraStmt->setString(2, User.GetBuffer(0));
OraStmt->setString(3, App.GetBuffer(0));
OraStmt->setString(4, Context.GetBuffer(0));         
OraStmt->registerOutParam(5,OCCICLOB);
OraStmt->registerOutParam(6,OCCISTRING,32767);
OraStmt->setAutoCommit(FALSE);

OraStmt->executeQuery(); 

但是,我无法定义第5个参数数据类型。我已经尝试过使用OCCICURSOR,OCCIREF和许多其他数据类型。

执行指令executeQuery()时,将使用ORA-6550抛出oracle :: occi :: SQLException。该ORA建议参数类型不匹配。

我以前曾在CLOB中写作,但从未通过REFCURSOR。

1 个答案:

答案 0 :(得分:0)

经过一些测试,解决方案是使用ResultSet作为中间。

OraStmt->setSQL("BEGIN :1 := SCHM.PCK.SAVE_CONF(:2,:3,:4,:5,:6); END;");
OraStmt->registerOutParam(1, OCCIINT);
OraStmt->setString(2, User.GetBuffer(0));
OraStmt->setString(3, App.GetBuffer(0));
OraStmt->setString(4, Context.GetBuffer(0));         
OraStmt->registerOutParam(5,OCCICLOB);
OraStmt->registerOutParam(6,OCCISTRING,32767);
OraStmt->setAutoCommit(FALSE);
OraStmt->executeUpdate(); 
ResultSet   *rs;

rs = OraStmt->getCursor(5);

rs->next();
Clob cLob = rs->getClob(1);

char *pBuffer = static_cast<char *>(Configuration.GetBuffer(0));

int remaining = Configuration.GetLength();
int written = 0;
int pos = 0;

do
{
    written = cLob.writeChunk(remaining, (unsigned char *)&pBuffer[pos], remaining, 1);

    pos += written;
    remaining -= written;
}
while(remaining > 0);

OraStmt->closeResultSet(rs);