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