可以有两种方式来bind lob字段

1)直接绑定lob 值

2)绑定lob locator指针

对于 直接绑定lob值的操作如下

char* sql = "insert into tab_lob(id,lob) values(:1,:2)";
OCIStmtPrepare(stmthp, errhp, sql, strlen(sql), OCI_NTY_SYNTAX, OCI_DEFAULT);
OCIBindByPos(stmtp, &bindhp[0], errhp, 1, &id, sizeof(int), SQLT_INT, 0, 0, 0, 0, OCI_DEFAULT);
OCIBindByPos(stmtp, &bindhp[1], errhp, 2, buffer, 3000, SQLT_LNG, 0, 0, 0, 0, OCI_DEFAULT);
OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL, OCI_DEFAULT);

但是这种方式有一个问题,就是只能绑定lob的大小小于4000字节,如果过大的话,会报错 ORA-24816。

另外一种方式就是绑定lob locator指针来操作

 

 1 OCILobLocator* lob;
 2 
 3 OCIBindByPos(stmthp, &bindhp[0], errhp, 1, &id, sizeof(int), SQLT_INT, 0, 0, 0, 0, OCI_DEFAULT);
 4 OCIDescriptorAlloc(envhp, &lob, OCI_DTYPE_LOB, 0, NULL);
 5 memset(temp, 'a', 30);
 6 c_len = 30;
 7 OCILobCreateTemporary(svchp, errhp, lob, OCI_DEFAULT, SQLCS_IMPLICIT, OCI_TEMP_CLOB, FALSE, OCI_DURATION_SESSION);
 8 OCILobWrite(svchp, errhp, lob, &c_len, 1, temp, 30, OCI_ONE_PIECE, NULL, NULL, OCI_DEFAULT, SQLCS_IMPLICIT);
 9 OCIBindByPos(stmthp, &bindhp[1], errhp, 2, &lob, sizeof(OCILobLocator*), SQLT_CLOB, 0, 0, 0, 0, OCI_DEFAULT);
10 OCIStmtExecute(svchp, stmthp, errhp, 1, 0, NULL, NULL, OCI_DEFAULT);

 

posted on 2017-06-29 15:22  dspeeding  阅读(2044)  评论(0编辑  收藏  举报