Dear StackOverflow'ers,
I've been getting into coding with C++ and I took a project where I read information out of a 4D SQL database into MySQL syntax .sql files which in turn get executed by an MySQL server. I'm running into the following problem; if I run the CreateSQL function with one table and then exit the programme, it runs fine.
If I loop the CreateSQL function to create SQL from all tables, it fails with a std::bad_alloc error.
Since I'm pretty new with C++, I was hoping if some of the more experienced C++ programmers could point me in the direction where this error could occur. My (unexperienced) guess would be incorrect freeing of variables or the timing of this, as follows:
SQLFreeHandle( SQL_HANDLE_STMT, hStmt ) ;
SQLFreeHandle( SQL_HANDLE_DBC, hConn ) ;
SQLFreeHandle( SQL_HANDLE_ENV, hEnv ) ;
Any help would be greatly appreciated.
The full source code is below:
EDIT: Following François' advice from the comments:
for( int i = 1 ; i <= numRows ; i++ )
{
// Datatypes
// SQLGetData
char buf[256];
SQLINTEGER numBytes ;
newFile << createInsert(table);
for( int j = 1 ;
j <= numCols ;
j++ )
{
retCode = SQLGetData(
hStmt,
j, // COLUMN NUMBER of the data to get
SQL_C_CHAR, // the data type that you expect to receive
buf, // the place to put the data that you expect to receive
255, // the size in bytes of buf (-1 for null terminator)
&numBytes // size in bytes of data returned
) ;
if( CHECK( retCode, "SqlGetData", false ) )
{
retCode2 = SQLDescribeColA( hStmt, j, colName, 255, &colNameLen, &dataType, &columnSize, &numDecimalDigits, &allowsNullValues ) ;
if( CHECK( retCode2, "SQLDescribeCol" ) )
{
//cout << dataType << endl;
if(dataType != 91) {
newFile << "'" << removeSlashes(removeSpecials(buf)) << "'";
}
else if (dataType == 91) {
newFile << "date_format(str_to_date('" << fixDate(buf) << "', '%d-%m-%Y'),'%Y-%m-%d')";
}
}
//Sleep(50);
}
if(j != numCols) {
newFile << ",";
}
}
newFile << ");\n";
cout << "Regel #" << i << " van tabel " << table << " is verwerkt." << endl;
retCode = SQLFetch( hStmt ) ;
if( !SQL_SUCCEEDED( retCode ) )
{
cout << "Tabel "+table+" is verwerkt." << endl;
printf( "Regel %d is de laatste regel.\n", i ) ;
}
}