in reply to Need an array as a function parameter
You are and you almost had it. Here is almost the same function with corrections.
sub insert { my ($self,$table,@values)=@_; my $query = sprintf("insert into %s values (%s);", $table,join(",",@values)); $dbh->do($query); }
Having said that I am wondering why you aren't doing something like:
The reason I am asking is you may get into quoting issues they way you are proceeding and the DBI interface takes care of that for you behind the scenes. Also using the "die" setup as I show it will allow you to (hopefully) catch errors.: : my $sql=sprintf("insert into %s values (%s);",$table, join(",",map{ "?" } @values)); my $sth=$dbh->prepare($sql) or die $dbh->errstr; $sth->execute(@values) or die $sth->errstr;
The other thing I find curious is the lack of field names in your insert statement. I'm not a SQL guru and I haven't tried what you are proposing but I've sorta taken it on faith that it is at least a good idea to provide the field names in an insert statement.
| Peter L. Berghold -- Unix Professional Peter at Berghold dot Net | |
| Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice. | |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Need an array as a function parameter
by CountZero (Bishop) on Mar 27, 2004 at 21:34 UTC | |
by optimist (Beadle) on Mar 27, 2004 at 23:15 UTC | |
|
Re: Re: Need an array as a function parameter
by stonecolddevin (Parson) on Mar 27, 2004 at 21:30 UTC | |
by blue_cowdawg (Monsignor) on Mar 27, 2004 at 21:39 UTC |