#!/usr/bin/perl -w # # When we work with databases, aren't there a few queries # we keep typing every day, over and over, to find out how # the database is going along? Well, since I'm too Lazy to # keep typing the same stuff, I wrote a tiny program to # take a number of queries and report its results # automatically. # # All there's to be done to add new queries is to fill in # the structures on the top of the program, and adjust to # your favorite RDBMS. Hopefully everything is self- # explanatory. # use DBI; use DBD::Pg; use CGI qw(:standard); use strict; use integer; print header (); my @qlist = ( { query => 'SELECT count(*) FROM item', desc => 'Number of items', labels => [ 'Number' ], }, { query => 'SELECT a, b, c FROM mytable', desc => 'stuff from mytable', labels => [ 'A', 'B', 'C' ], }, ); my $dbh = DBI->connect ("dbi:Pg:dbname=beta", 'cbraga', '', { AutoCommit => 1, RaiseError => 1 } ) || die "error connecting to db"; foreach my $query (@qlist) { print "

$query->{desc}

\n"; my $sth = $dbh->prepare ($query->{query}); $sth->execute (); unless ($sth->rows) { print "No results.

\n"; next; } print "\n"; my $line = $query->{labels}; foreach my $label (@$line) { print ""; foreach my $col (@$row) { print "
$label \n"; } while (my $row = $sth->fetchrow_arrayref) { print "
$col\n"; } } print "
\n"; } $dbh->disconnect ();