Immediate Execution

To execute an SQL statement immediately, use the member function IldRequest::execute, which takes two arguments:

  • The first argument is the SQL statement string.

  • The second argument, rowCount, is optional. If specified, it must be a valid pointer to an IlInt variable, which will be set to the number of processed rows if the statement is delete, insert, or update.

Note

rowCount is zero after a select query is executed. For performance reasons, most RDBMSs do not “look ahead” to set this value for a query.

Once the call to IldRequest::execute succeeds, you can retrieve the execution status via the member function IldRequest::getStatus. This member function returns the number of processed rows if the SQL statement is delete, insert, or update. If it is a select statement, the returned value is the number of rows actually fetched —namely 0 when the execution has just been completed.

Note

An SQL statement that is to be executed immediately must not contain any parameters.

If the execution fails, the returned value, usually -1, is not meaningful.

{

if (!request->execute(queryBuffer))

IldDisplayError("Executing: ", request);

else {

if (!request->fetch())

IldDisplayError("Fetching: ", request);

else if (request->hasTuple())

// Loop with two levels, since ODBC, Sybase, and MS SQL Server

// can have several result sets for one command.

do {

if (request->getColCount()) {

IldPrintTuple(request, IldNames);

IldPrintTuple(request, IldSeparators);

IldPrintTuple(request);

while (request->fetch().hasTuple())

IldPrintTuple(request);

cout << endl;

if (request->isErrorRaised())

IldDisplayError("Fetching: ", request);

}

} while (request->fetch().hasTuple());

else if (request->getColCount()) {

IldPrintTuple(request, IldNames);

IldPrintTuple(request, IldSeparators);

cout << endl << "No row found." << endl;

}

else if ((count = request->getStatus()) > 0)

cout << count << " modified line(s)" << endl;

}

}

It is also possible to get the attributes of the returned rows from the result set, using the following member functions:

If the SQL statement is select, you can retrieve the rows using the member function IldRequest::fetch. When used after execution of any other SQL statement, this member function is ineffective, but no error is raised.