[Melbourne-pm] Perl DBI reference recommendations

Sam Watkins sam at nipl.net
Thu Jul 16 00:38:51 PDT 2009

> I want a handy 'desk' reference with
> a) Lots of examples
> b) Easy to read and complete reference material
> The reason is that I can't hold the details in my head (because I do
> not DBI regularly) , but I need to keep knocking off dirty that little
> scripts that run sql queries. I figure the easiest way for that is SQL
> via DBI rather than an abstraction module.

Here is a little "handy desk reference" for you, covering all of DBI
that you should use.  It is shorter than the manpage or a book ;)

my $dbh =
$username, $password);
my $sth = $dbh->prepare("SELECT foo, bar FROM baz WHERE foo = ? AND bar
= ?");
$sth->execute("foovalue", $barvalue);
while (my $row = $sth->fetchrow_arrayref) {
  my ($foo, $bar) = @$row;
  print "$foo  $bar\n";

You should rarely need to use anything other than the above from DBI. 
keep it simple!

I highly recommend using the "bind values" stuff with the ?s in the SQL
and corresponding parameters to execute.  DBI will quote and escape
values for you correctly so you don't need to worry about SQL injection
attacks, etc.

DBI has umpteen different fetch methods, your life will be easier if you
just use one of them.



More information about the Melbourne-pm mailing list