Manual error checking
Of course, the DBI still allows you to manually error check your programs and the execution of DBI methods. This form of error checking is more akin to classic C and Perl programming, where each important statement is checked to ensure that it has executed successfully, allowing the program to take evasive action upon failure.
DBI, by default, performs basic automatic error reporting for you by
enabling the PrintError attribute. To disable this
feature, simply set the value to 0 either via the
handle itself after instantiation, or, in the case of database
handles, via the attribute hash of the connect( )
method.
For example:
### Attributes to pass to DBI->connect( )
%attr = (
PrintError => 0,
RaiseError => 0
);
### Connect...
my $dbh = DBI->connect( "dbi:Oracle:archaeo", "username", "password" , \%attr );
### Re-enable warning-level automatic error reporting...
$dbh->{PrintError} = 1;Most DBI methods will return a false status value, usually
undef, when execution fails. This is easily tested
by Perl in the following way:
### Try connecting to a database
my $dbh = DBI->connect( ... )
or die "Can't connect to database: $DBI::errstr!\";The following program disables automatic
error handling, with our own tests to check for errors. This example
also moves the attributes into the connect( )
method call itself, a clean style that’s commonly used:
#!/usr/bin/perl -w # # ch04/error/ex1: Small example using manual error checking. use DBI; # Load the DBI module ### Perform ...