• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 133
  • Last Modified:

count

how can I get the total number of records in a table of a database?
i would like to use PERL, DBI.
any one can provide me a simple source code?

0
stanleyhuen
Asked:
stanleyhuen
  • 3
1 Solution
 
yovavm101799Commented:
1. Connect to your DB.

2. Perform a 'select* from Table'

3. Get the meta-data $DBI::rows
This variable holds the number of rows affected or returned by the last executed statement

Sample:
------
use DBI;
$dbname = 'DEMO'; $user = 'admin';
$password = 'pass'; $dbd = 'Oracle';

$dbh = DBI->connect($dbname,$user,$password,$dbd) ||
       die "Error connecting $DBI::errstr\n";;


$sth = $dbh->prepare("Select * from Customers");
if ($DBI::err) {
  die STDERR "$DBI::errstr\n";
}
$sth->execute() ;
if ($DBI::err) {
  die STDERR "$DBI::errstr\n";
}
$numOfRecs = $DBI::rows;
print "Total records in table: Customer is $numOfRecs\n";


0
 
mwhuenCommented:
besides  $DBI::rows, $DBI::err, $DBI::errstr, any other useful functions of DBI?
where can I get more information of these useful functions?
0
 
yovavm101799Commented:
In case you mean MetaData, well here are some methods used to obtain Meta Data:

$dbh->tables()
fetch a list of all accessible table names

$DBI::rows
The number of rows affected or returned

After executing the statement, use the statement handle to get:

$sth->{NUM_FIELDS}
The number of fields returned by a select

$sth->{NUM_PARAMS}
The number of parameters returned by any query

After a select query, the following attributes contain references to arrays of field-specific information:

$sth->{NAME}
Column names returned by the query

$sth->{NULLABLE}
Booleans indicating whether fields are nullable or not

$sth->{TYPE}
Field types

$sth->{PRECISION}
Floating-point precision of field

$sth->{SCALE}
Field lengths

0
 
stanleyhuenAuthor Commented:
thanks yovavm,

i would like to learn more about DBI?

do u know any good resources of DBI?
0
 
yovavm101799Commented:
Check: "Advanced Perl Programming", By Sriram Srinivasan, published by O'Reilly & Associates.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now