Pascal Data Objects

function IPDOStatement.getNumberRows: Int64;

IPDOStatement.getNumberRows returns the number of rows returned by a SELECT statement. This behaviour is not guaranteed for all databases and should not be relied on for portable applications.

Examples

Example 1. Return the number of selected rows

procedure example1;
var
   count: Int64;
begin
  stmt = db.query_as_is('SELECT * FROM fruit');
  stmt.execute;
  count := stmt.getNumberRows;
  writeln (format('Number of rows that were selected: %d',[count]));
end;

The above example will output:

Number of rows that were selected: 167

Example 2. Counting rows returned by a SELECT statement

For most databases, IPDOStatement.getNumberRows does not return the number of rows affected by a SELECT statement. Instead, use TPDO.query_as_is to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then retrieve the number of rows that will be returned. Your application can then perform the correct action. Note: When MySQL is set to PREFETCH the result set, the IPDOStatement.getNumberRows result is accurate.

procedure example2;
var
   sql: AnsiString;
begin
  sql := 'SELECT COUNT(*) FROM fruit WHERE calories > 100';
  stmt := db.query_as_is(sql);
  stmt.fetch(row);
  if (row.column[0].asInt64 > 0) then   
    begin
       sql := 'SELECT name FROM fruit WHERE calories > 100';
       stmt := db.query_as_is(sql);
       while (stmt.fetch(row)) do 
          writeln ('name = ' + row.hash('name').asString);
    end
  else
      writeln ('No rows matched the query.');
end;

The above example will output:

apple
banana
orange
pear