[Melbourne-pm] Perl DBI reference recommendations

Alfie John alfiejohn at gmail.com
Thu Jul 16 04:16:41 PDT 2009


Hey all,

Sorry, I thought you were interested in some leasure time reading. The DBI
book is nice for that. If what you're really after is something like a ref
card, the SYNOPSIS of 'perldoc DBI' is probably all you need.

If what you're really after is cheap scripts and your data set isn't huge,
the easiest way is:

  my $rows = $dbh->selectall_arrayref( $query, undef, @values );

No prepare, bind or execute needed.

If you're ever interested in what's under the hood, the DBI source is
actually quiet readable. As for Class::DBI vs DBIx::Class, I won't rehash
the flamewar here. Saying that, going straight DBI vs ORM should really
depend on what you're doing. When you're mostly doing CRUD and accessing the
same tables over and over again, then ORM is definately the goer. But if
you're mostly doing custom queries and reporting and never repeating
yourself, I would lean more to straight SQL.

Alfie

On Thu, Jul 16, 2009 at 5:57 PM, Tim Evans <tim.evans at sputnikagency.com>wrote:

> I would add to sam's list ...
>
>        $dbh->{mysql_enable_utf8} or die "couldn't init mysql_enable_utf8";
>
> And as long as the db is set  up to support utf8
>
>        $dbh->do("SET character_set_database=utf8");
>        $dbh->do("SET character_set_server=utf8");
>        $dbh->do("SET names 'utf8'");
>        etc.....
>
>
> On 16/07/09 5:38 PM, "Sam Watkins" <sam at nipl.net> wrote:
>
> >> 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 =
> > DBI->connect("DBI:mysql:database=$database;host=$hostname;port=$port",
> > $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";
> > }
> > $sth->finish;
> >
> >
> > 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.
> >
> > regards,
> >
> > Sam
> > _______________________________________________
> > Melbourne-pm mailing list
> > Melbourne-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/melbourne-pm
>
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20090716/bbd1cb3b/attachment.html>


More information about the Melbourne-pm mailing list