How do I implement a select count (*) in DBTools.h++?

Article ID: 800
Last updated: 29 Jan, 2008
Article ID: 800
Last updated: 29 Jan, 2008
Revision: 1
Views: 4609
Posted: 26 Nov, 1997
by --
Updated: 29 Jan, 2008
by
Problem


 

As a database programmer, you may want to get a count of the total number of rows
in a table or the number of rows that meet certain criteria.



Cause


 

With rowCount() method available in the RWDBResult class you can only get the rows inserted or updated or deleted
but not the rows selected.  



Action


Here is a code snippet demonstrating how to implement a row count using the DBTools.h++ selector object.

      RWDBTable table = db.table(tableX);
      RWDBSelector select = db.selector();
      select << rwdbCount(table[anycolumn]);

The above statements will produce a SQL statement like:

     SELECT COUNT(table.anycolumn)

You can also use this form:

     select << rwdbCount();
     select.from(table);

OR

     select.from(tableX);

and the SQL statement produced will look like this:

     SELECT COUNT(*) FROM tableX t0

You can also add a where clause with any criteria desired by using the where() method on the selector.

Now you have to use RWDBReader to read the number of rows selected.

RWDBReader rdr = select.reader(conn);
int numberOfRows;
while(rdr())
{
  rdr >> numberOfRows;
  cout << numberOfRows << endl;
}

This article was:   Helpful | Not helpful
Report an issue
Article ID: 800
Last updated: 29 Jan, 2008
Revision: 1
Views: 4609
Posted: 26 Nov, 1997 by --
Updated: 29 Jan, 2008 by

Others in this category