Home » Developer & Programmer » Precompilers, OCI & OCCI » Error ORA-01480: trailing null missing from STR bind value (OCCI v19 windows)
Error ORA-01480: trailing null missing from STR bind value [message #687681] Thu, 04 May 2023 08:13 Go to next message
thmdevt
Messages: 1
Registered: May 2023
Junior Member
0


I try to insert data into a table with OCCI C++.

I got some examples and tried to report them in my program without success.

My problem is that I have std::string(s) in my data model and I need to convert them into 2D char arrays to be used by the setDataBuffer method.

I cannot find where is the wrong code because I first allocate the rows of the 2D arrays and for each row I allocate the size of the string + 1 (for the null char) and the function std::strcpy normally adds the null char to the destination.

I cannot understand why Oracle says that I give a buffer without null char !

Here is my code, everything works fine until the line:

statement->executeArrayUpdate(numberOfLinks)

// Open a database connection
oracle::occi::Connection* connection = LBCDatabase::instance().getConnection();

// Structures to store results
int numberOfLinks = model.numberOfLinksOfType(linkType) / 2;

char** simulationIdBuffer = new char*[numberOfLinks];
char** linkIdBuffer = new char*[numberOfLinks];
char** linkTypeBuffer = new char*[numberOfLinks];

unsigned short* simulationIdBufferLength = new unsigned short[numberOfLinks];
unsigned short* linkIdBufferLength = new unsigned short[numberOfLinks];
unsigned short* linkTypeBufferLength = new unsigned short[numberOfLinks];

// Simulation id is common for all links
std::string simulationId = model.simulationId();

// Loop on FWD links
int linkIndex{ 0 };
for (const std::shared_ptr<Link>& link : model.links())
{
    // Skip RTN links and FWD links UP
    if (ELinkType::RTN == link->linkType() ||
        (ELinkType::FWD == link->linkType() && ELinkSide::UP == link->linkSide()))
        continue;

    // Get datas for current link
    std::string linkId = LBCUtils::restoreLinkId(link->linkId());
    std::string linkType = LBCUtils::enumToString<ELinkType>(link->linkType());

    // Add them into buffers
    simulationIdBuffer[linkIndex] = new char[simulationId.length() + 1];
    std::strcpy(simulationIdBuffer[linkIndex], simulationId.c_str());

    linkIdBuffer[linkIndex] = new char[linkId.length() + 1];
    std::strcpy(linkIdBuffer[linkIndex], linkId.c_str());

    linkTypeBuffer[linkIndex] = new char[linkType.length() + 1];
    std::strcpy(linkTypeBuffer[linkIndex], linkType.c_str());

    // Store the length
    simulationIdBufferLength[linkIndex] = simulationId.length() + 1;
    linkIdBufferLength[linkIndex] = linkId.length() + 1;
    linkTypeBufferLength[linkIndex] = linkType.length() + 1;

    ++linkIndex;
}

// Create the query
std::string query("INSERT INTO res_link ");
query.append("(simulation_id, link_id, link_type) ");
query.append("VALUES(:1, :2, :3)");

oracle::occi::Statement* statement = connection->createStatement(query);
statement->setDataBuffer(1, (dvoid*)simulationIdBuffer, oracle::occi::OCCI_SQLT_STR, sizeof(simulationIdBuffer[0]), simulationIdBufferLength);
statement->setDataBuffer(2, (dvoid*)linkIdBuffer, oracle::occi::OCCI_SQLT_STR, sizeof(linkIdBuffer[0]), linkIdBufferLength);
statement->setDataBuffer(3, (dvoid*)linkTypeBuffer, oracle::occi::OCCI_SQLT_STR, sizeof(linkTypeBuffer[0]), linkTypeBufferLength);

// Execute the query
statement->executeArrayUpdate(numberOfLinks);   // Exception here: ORA-01480: trailing null missing from STR bind value
connection->commit();

// Cleanup buffers
delete[] simulationIdBufferLength;
delete[] linkIdBufferLength;
delete[] linkTypeBufferLength;
LBCUtils::deleteCharBuffer(simulationIdBuffer, numberOfLinks);
LBCUtils::deleteCharBuffer(linkIdBuffer, numberOfLinks);
LBCUtils::deleteCharBuffer(linkTypeBuffer, numberOfLinks);

// Cleanup before to go out
connection->terminateStatement(statement);
LBCDatabase::instance().releaseConnection(connection);
When I log the content of my arrays the output is correct.

Thanks a lot if somebody can help me... Regards.
Re: Error ORA-01480: trailing null missing from STR bind value [message #687682 is a reply to message #687681] Thu, 04 May 2023 10:25 Go to previous message
Michel Cadot
Messages: 68597
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You can have this error when the value is too large to fit in a table column.

Previous Topic: Get OCCI exe UNIX PID in SQL/PLSQL
Goto Forum:
  


Current Time: Mon Feb 26 02:07:36 CST 2024