Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: perl and psql

by bv (Friar)
on Dec 02, 2009 at 14:29 UTC ( #810570=note: print w/replies, xml ) Need Help??


in reply to Re^2: perl and psql
in thread perl and psql

There was a bug in the code that was posted as a reply. I don't think Xilman intended that code to be blindly copy-and-pasted, but here's what's probably going on:

# This should not have the '$dbh->' at the beginning, but the real iss +ue is # $cluster here is a scalar, but foreach my $cluster (@clusters) { # here, they are being treated as arrays (if they were interpolated as + written. # $dbh->do ("INSERT INTO $table (start, end) VALUES (cluster[0], clust +er[1]);"); # a better approach, if your DBI module supports placeholders: $dbh->do ("INSERT INTO $table (start, end) VALUES (?,?);", @{$cluster} +) # or even: $dbh->do ("INSERT INTO $table (start, end) VALUES (?,?);", +$cluster->[0], $cluster->[1]) }

Again, this is untested code, and may not work as advertised. If the -> and @{} are confusing you, check out perlreftut for more info on references.


@_=qw; Just another Perl hacker,; ;$_=q=print "@_"= and eval;

Replies are listed 'Best First'.
Re^4: perl and psql
by almut (Canon) on Dec 02, 2009 at 15:59 UTC
    $dbh->do ("INSERT INTO $table (start, end) VALUES (?,?);", @{$cluster})

    When you use do with placeholders and bind values, you also need to specify the \%attr hashref (for syntactical reasons, because the synopsis is $dbh->do($statement, \%attr, @bind_values)).  You may use undef if you don't have any attributes.

    $dbh->do ("INSERT INTO $table (start, end) VALUES (?,?)", undef, @$clu +ster)
Re^4: perl and psql
by Xilman (Friar) on Dec 02, 2009 at 17:11 UTC

    Thank you. It was an unfortunate cut&paste error on my part.

    I'm well aware, of course, that place holders are a Good Idea(tm). The original Perl script from which I made the c&p used them but I removed them from the posted snippet to simplify it.

    As I said originally, the OP should take my fragment as a hint for a learning exercise and not as a complete answer.

    Paul

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://810570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2019-12-14 15:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?