Re: how to allow variable number of argumets in mysql insert query
"Wagner, Chris (GEAE, CBTS)" <[email protected]> Tue, 18 Dec 2007 19:49:18 -0500
| Newsgroups | gmane.comp.db.mysql.perl |
|---|---|
| Organization | GE Aircraft Engines \Cincinnati Bell |
| Message-ID | <[email protected]> |
Hi. This way will let u take advantage of prepare_cached and be totally
flexible. $dataref is a reference to a hash whose keys are the column
names and whose values are the field values to insert. $fieldref is an
array reference containing the column names i.e. hash keys.
sub insert {
my ($fieldref, $dataref) = @_ or return undef;
$handle = $dbh->prepare_cached( 'INSERT INTO `table` (`' .
join("`, `", @{$fieldref}) .
'`) VALUES (' .
join(", ", ("?") x scalar @{$fieldref}) .
')' );
$handle->execute(map {$dataref->{$_}} @{$fieldref})
or (print $dbh->errstr and return undef);
}
fungazid wrote:
>
> Help help help please
>
> IâÂÂm using DBD::mysql, where Insert query looks like:
>
> ______________________________________________________________
> my $str= âÂÂ?,?,?,,,âÂÂ;
> $q=$dbh->prepare("INSERT INTO $table VALUES($str)")
> or die "Couldn't prepare statement: " . $dbh->errstr;
>
> $q->execute($args1[0],$args1[1],$args1[2],â¦)
> or die "Couldn't execute statement: " . $q->errstr;
> ________________________________________________________________
>
> THE PROBLEM:
> If I want to change the number of arguments sent to execute(), I must change
> the source code.
> I would like to be able to change it on run-time with something like:
--
Chris Wagner
CBTS
GE Aircraft Engines
[email protected]
--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/[email protected]