Handling Query Errors

It can be useful to control DBI's error-handling behavior for a particular database connection. The connection method looks like this:

$dbh = DBI->connect ($data_source, $username, $auth, %attr);

The last attribute, a hash, can be left unspecified (as we've done until now), or can be specified, like this, for example:

$dbh = DBI->connect ($data_source, $username, $auth,
  { RaiseError => 0, PrintError => 1} );

RaiseError and PrintError are error-handling attributes that determine how DBI should handle errors in queries within that connection:

  • RaiseError being set to 1 causes the Perl script to exit with an error message if a DBI error occurs. Default = 0 (off).

  • PrintError being set to 1 generates an error message if a DBI ...

Get Sams Teach Yourself MySQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.