[Oracle]ocilib绑定数组实现批量写入

#include "ocilib.h"



#define INSERT_SQL_00    MT("INSERT INTO C_PICRECORD_00 (ID, DEV_ID) VALUES (:id, :devid)")

#define CHECK_SUCCESS(ret, x, err, clear)        ret = x;if(ret < 1){GET_LAST_ERROR(err);goto clear;}



int CFLOracleOCIVisitor::insertVehRecords(DB_Write_info_t** dbInfo, int count)

{

    if(isConnected() != 0)

        return -1;



    int nRet = 0;

    std::string strTmp;

    OCI_Statement* stm = OCI_StatementCreate( m_conn);

    if(NULL == stm)

    {

        m_Error = OCI_GetLastError();

        return -3;

    }

    uint32* tab_id = new uint32[count];

    memset(tab_id, 0, count);

    char* tab_devid = new char[count*64];

    memset(tab_devid, 0, count*64);

    //Prepare        

    CHECK_SUCCESS(nRet, OCI_Prepare(stm, INSERT_SQL_00), m_Error, clear);

    // binding

    CHECK_SUCCESS(nRet, OCI_BindArraySetSize(stm, count), m_Error, clear);

    OCI_BindArrayOfUnsignedInts(stm, ":id", (uint32*)tab_id, 0);

    OCI_BindArrayOfStrings(stm, ":devid", (char*)tab_devid, 63, 0);

    for(int i = 0; i < count; i++)

    {

        tab_id[i] = dbInfo[i]->dbId;

        memcpy(tab_devid+i*64, dbInfo[i]->devId, 64);

    }

    //execute

    CHECK_SUCCESS(nRet, OCI_Execute(stm), m_Error, clear);

    CHECK_SUCCESS(nRet, OCI_Commit(m_conn), m_Error, clear);



clear:

    OCI_StatementFree(stm);



    delete[] tab_id;

    delete[] tab_devid;

    return nRet;

}

 

你可能感兴趣的:(oracle)