#!/usr/bin/perl use strict; use DBI; my $dbh; my $sth; my $sql = "select count(*) from readings"; my $status; my @row; my @rows; my $count = 0; my $arr_ref; my $debug = 1; # # xxx low security environment # my $username = "schulra"; my $password = "ch\@ngeme"; my $dbname = "weather"; my $host = "cs.earlham.edu"; # # xxx Don't use die # $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$host", $username, $password) || die "dbi-example.pl - Can't connect, $DBI::errstr"; print "Connected\n"; $sth = $dbh->prepare($sql) || die "dbi-example.pl - prepare error: ($sql) $DBI::errstr"; $status = $sth->execute || die "dbi-example.pl - execute error: ($sql) status=$status $DBI::errstr"; # # There is a built-in DBI method for getting the number of rows in a # result set, rather than incrementing $count in this loop. Since we are # processing the rows one at a time anyway it doesn't matter much in this # case. # # The DBI API also supports fetching all the rows in one call, and individual # columns, and both of those notions simultaneously, or all rows and all # columns. # while (@row = $sth->fetchrow_array) { $rows[$count] = [ @row ]; $count++; } if ($debug > 0) { print "dbi-example.pl - count=$count\n"; for $arr_ref ( @rows ) { print "dbi-example.pl - @$arr_ref \n"; } } $dbh->disconnect(); exit;