John suggests:
> My first thought is to string multiply by the number of elements:
>
> JB1: perl -e "print('?',',?'x (@ARGV-1));" a b c d e f
> ?,?,?,?,?,?
>
> or
>
> JB1: perl -e "print('?',',?'x (split(',',$ARGV[0])-1));" "a,b,c,d,e,f"
> ?,?,?,?,?,?
>
> P.S. I get this on the latter...
I often do the following, using the keys function on a hash of names and
values
in combination with map.
Assuming an HASH of names with values;
my %H = (cid =>q(1234), name=>q(bob) , type => q(Big Spender));
my $insert = sprintf ( qq(insert into custtab (%s) values (%s)),
keys %H,
join ( map ( q(?), keys %H)));
# Now you have $insert = 'insert into custtab (name, cid, type) values (
?, ?, ? )'
my $sth=$dbh->prepare($insert);
my $count=1;
map ( $sth->bind_param($count++, $_), values %H );
$sth->execute
$sth->finish;
$dbh->disconnect;
exit;
I'm coding this up from memory, so hopefully there aren't too many bugs...
BTW: This works if all the fields are of the default type. If not, you've
got to keep track
of meta data about the fields and use the three argument form of bind_param,
but that's not to hard to do.
Stph