Rogue Wave DB Link Tutorial > Rogue Wave DB Link Basic Use > Step 2: Querying the Database
 
Step 2: Querying the Database
To send a query to the database, you need to get an instance of the IldRequest class. This class is used both to send queries to the RDBMS and to retrieve results. The instance is created using the IldDbms::getFreeRequest method.
After connecting to the RDBMS as described in Step 1, a new request is allocated from the connection (this request will be used later to send queries to the RDBMS). This is done as follows:
IldRequest* request = dbms->getFreeRequest() ;
if (dbms->isErrorRaised()) {
IldDisplayError("Creation of request failed : ", dbms) ;
delete dbms ;
exit(1) ;
}
Note: The error handling mechanism is the same as the one used with the IldDbms instance in Step 1.
Then, the table is created by using this new IldRequest instance to send a DDL (Data Definition Language) statement to the RDBMS. This is done using the IldRequest method execute (const char*, IldInt* rowCount = 0), as follows:
const char* createStr = "create table ATABLE(F1 int,F2 char(20))" ;
cout << "Creating a table : " << createStr << endl ;
request->execute(createStr) ;
if (request->isErrorRaised()) {
IldDisplayError("Table creation failed : ", request) ;
delete dbms ;
exit(1) ;
}
The second argument of the execute method is an optional output argument. It is used to get the number of rows modified by the statement. It is not applicable for a DDL statement but it can be used for a DML (Data Manipulation Language) statement, such as insert.
An insert statement now writes records to the table (this is the simplest way to insert a row in a table).
const char* insertStr1 = "insert into ATABLE values(40,'Forty')" ;
IldInt nbRows = 0 ;
cout << "Row #1 : " << insertStr1 << endl ;
if (!request->execute(insertStr1, &nbRows))
IldDisplayError("Insertion failed :", request) ;
else cout << "\t" << nbRows << " row inserted." << endl ;
The number of inserted rows is retrieved with the second parameter of the IldRequest::execute method. This is useful mainly when running an update statement together with a where clause. In such a case, you may not know how many rows are updated. The only way to be made aware of this is to use this second parameter.
This time the error was checked using the '!' unary operator. It is redefined by the IldIldBase class to return the error status. Therefore, it may be used with either an IldDbms or an IldRequest instance. It is equivalent to the isErrorRaised method, except that it is shorter to write.
Before leaving the program, the table that has just created must be cleaned out. This is done the same way it was created— by using a drop statement.
const char* dropStr = "drop table ATABLE" ;
cout << "Dropping table : " << dropStr << endl ;
if (!request->execute(dropStr))
IldDisplayError("Drop table failed : ", request) ;
Conclusion
These instructions are all that is needed to send a query to the RDBMS. They will be used often.
See source code.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.