|
|
|
|
| |
my %attr =
(
PrintError => 0,
RaiseError => 0
);
my $dbh = DBI->connect ($dsn, $user_name, $password, \%attr)
or die "Could not connect to server: $DBI::err ($DBI::errstr)\n";
The following script, dump_members2.pl, illustrates how you might write a script when you want to check for errors yourself and print your own messages. dump_members2.pl processes the same query as dump_members.pl but explicitly disables PrintError and RaiseError and then tests the result of every DBI call. When an error occurs, the script invokes the subroutine bail_out() to print a message and the contents of $DBI::err and $DBI::errstr before exiting:
#! /usr/bin/perl -w
# dump_members2.pl - dump Historical League's membership list
use strict;
use DBI;
my $dsn = "DBI:mysql:sampdb:cobra.snake.net"; # data source name
my $user_name = "sampadm"; # user name
my $password = "secret"; # password
my %attr = # error-handling attributes
(
PrintError => 0,
RaiseError => 0
);
# connect to database
my $dbh = DBI->connect ($dsn, $user_name, $password, \%attr)
or bail_out ("Cannot connect to database");
# issue query
my $sth = $dbh->prepare ("SELECT last_name, first_name, suffix, email,"
. "street, city, state, zip, phone FROM member ORDER BY last_name")
or bail_out ("Cannot prepare query");
$sth->execute ()
or bail_out ("Cannot execute query");
# read results of query
while (my @ary = $sth->fetchrow_array ())
{
print join ("\t", @ary), "\n";
}
|
|
|
|
|
|
| Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist |
|