I’m writing a subroutine for DBI updates, and are having some trouble figuring out how to add placeholder and stuff…
I have this:
sub row_update { my $table = shift; my %updates = @_; my $placeholders = ... $dbh->do('UPDATE $table SET (foo) WHERE (bar)') etc... }
Any ideas?
I just want a simple update function where I can send in x number of arguments (as a hash).
Thanks in advance!
If I understand the question correctly, it sounds like you’re after SQL::Abstract. First, we create an
SQL::Abstractobject:Now, as an example, we’ll use it to insert some data into a table:
This results in:
The nice thing about this is we can pass it directly to DBI:
Of course, you want to be updating records, not just inserting them. Luckily, this is also quite easy:
This produces:
If you’re after more information about
SQL::Abstract, I recommend you look at its CPAN page. There’s also a chapter in Perl Training Australia‘s Database Programming with Perl manual, which are freely available from our course notes page.All the best,
Paul
Disclaimer: I’m managing director of Perl Training Australia, and therefore think that our course notes are pretty good.