1: #!/usr/bin/perl -w
2:
3: #
4: # When we work with databases, aren't there a few queries
5: # we keep typing every day, over and over, to find out how
6: # the database is going along? Well, since I'm too Lazy to
7: # keep typing the same stuff, I wrote a tiny program to
8: # take a number of queries and report its results
9: # automatically.
10: #
11: # All there's to be done to add new queries is to fill in
12: # the structures on the top of the program, and adjust to
13: # your favorite RDBMS. Hopefully everything is self-
14: # explanatory.
15: #
16:
17: use DBI;
18: use DBD::Pg;
19: use CGI qw(:standard);
20:
21: use strict;
22: use integer;
23:
24: print header ();
25:
26: my @qlist = (
27: {
28: query => 'SELECT count(*) FROM item',
29: desc => 'Number of items',
30: labels => [ 'Number' ],
31: },
32: {
33: query => 'SELECT a, b, c FROM mytable',
34: desc => 'stuff from mytable',
35: labels => [ 'A', 'B', 'C' ],
36: },
37: );
38:
39: my $dbh = DBI->connect ("dbi:Pg:dbname=beta", 'cbraga', '', { AutoCommit => 1, RaiseError => 1 } ) || die "error connecting to db";
40:
41: foreach my $query (@qlist) {
42: print "<h1> $query->{desc} </h1>\n";
43:
44: my $sth = $dbh->prepare ($query->{query});
45: $sth->execute ();
46:
47: unless ($sth->rows) {
48: print "No results. <p>\n";
49: next;
50: }
51:
52: print "<table border> <tr>\n";
53: my $line = $query->{labels};
54: foreach my $label (@$line) {
55: print "<td> <b> $label </b>\n";
56: }
57:
58: while (my $row = $sth->fetchrow_arrayref) {
59: print "<tr>";
60: foreach my $col (@$row) {
61: print "<td> $col\n";
62: }
63: }
64:
65: print "</table>\n";
66: }
67:
68: $dbh->disconnect ();