#!/usr/local/bin/perl -w

# Sample database access:

use strict;
use DBI;

my $titlecol=0;
my $lastnamecol=1;
my $firstnamecol=2;
my $pubyearcol=3;
my $urlcol=4;
my $publisheridcol=5;

# Handle to database.  This is for a local socket/file connect,
# but the syntax is similar for a network connect: 
my $dbh = DBI->connect('dbi:mysql:database=pg;file=/var/lib/mysql/mysql.sock', 'pg', 'cs205', { RaiseError => 1} );

my $query="show tables";

# Run a query, forego output:
#my $result = $dbh->do($query);

# Run a query, get output in $row
my $rows = $dbh->selectall_arrayref($query);
my $ctr=0;
foreach my $row (@$rows) {
    print "#" . ++$ctr . "=" . $row->[0] . "\n";
}

# Let's get all the values from a table:
$query="select * from books";
my $rows = $dbh->selectall_arrayref($query);

$ctr=0;
print "\tTitle\tFirstname Lastname\tPubyear\tURL\tPublisherID\n";
foreach my $row (@$rows) {
    print "#" . ++$ctr . "\t" .
	$row->[$titlecol] . "\t" .
	$row->[$firstnamecol] . " " .
	$row->[$lastnamecol] . "\t" .
	$row->[$pubyearcol] . "\t" .
	$row->[$urlcol] . "\t" .
	$row->[$publisheridcol] . "\n";
}

#foreach my $i ( $row ) { print $row{$i} ; }

# Done:
$dbh->disconnect;
exit;



