viernes, 5 de febrero de 2010

Mysql from Perl

There's a lot of texts written about perl+sql . In fact there's a whole O'Reilly book about this.

But in perl, often the TIMTOWTDI motto fuzzies me a bit. So here's what I've chosen to use for my simple Mysql accesses (For complex and scalable things, there's DBIx::Class).

For small updates, or writting tests, you could go with plain DBD::DBI, but DBIx::Simple seems to fit better in my brain rules. There are plenty of well thought examples here.
my $dbh = DBIx::Simple->new(
$dsn,
$user,
$pwd, {RaiseError => 1}
);
#my $sql = SQL::Abstract->new;
my @fields = qw(codi A Br P S SA ZN e d);
my $result = $dbh->select('vGRID', \@fields, { idExp => $idExp, error => 0 }) ;

It's pretty confortable being able to use select, update, etc. as methods, and not bundled into strings. SQL::Abstract does a good job here too, giving the option to build hashes dinamically and passing to the select method.