DapperDan has asked for the wisdom of the Perl Monks concerning the following question:
In the values section of the SQL statement, I need a string of the form "?,?,?" where there is a ? for each field, and all the ?s are delimited by commas. However, there can not be a trailing comma at the end or syntax errors in the database (PostgreSQL) result.
I tried
my $sth = $dbh->prepare( 'INSERT INTO table(' . join(',', keys %$c) . ") VALUES (" . chop(join('', map {'?,'} keys %$c)) . ")" );
I managed to make the code work by taking it out and explicitly assigning the value of the expression to a scalar, $placeholderstr. I would like to inline the rvalue of the first line of this code in the $dbh->prepare() statement. Any ideas?
code:
my $placeholderstr = join('', map {'?,'} keys %$c); chop $placeholderstr; my $sth = $dbh->prepare( 'INSERT INTO table(' . join(',', keys %$c) . ") VALUES ($placeholderstr)" ); $sth->execute(values %$c) or die $dbh->errstr;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can't I chop the lvalue of join?
by japhy (Canon) on Sep 28, 2002 at 14:19 UTC | |
by DapperDan (Pilgrim) on Sep 28, 2002 at 14:33 UTC | |
by japhy (Canon) on Sep 28, 2002 at 15:36 UTC | |
by Aristotle (Chancellor) on Sep 28, 2002 at 18:07 UTC | |
|
Re: Can't I chop the lvalue of join?
by broquaint (Abbot) on Sep 28, 2002 at 14:29 UTC | |
|
Re: Can't I chop the lvalue of join?
by Aristotle (Chancellor) on Sep 28, 2002 at 18:23 UTC | |
|
Re: Can't I chop the lvalue of join?
by chromatic (Archbishop) on Sep 28, 2002 at 16:26 UTC | |
|
Re: Can't I chop the lvalue of join?
by bart (Canon) on Sep 29, 2002 at 11:45 UTC |