Re: how to allow variable number of argumets in mysql insert query

fungazid <[email protected]> Wed, 19 Dec 2007 11:44:14 -0800 (PST)
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
Chris,
you are a genius ! nothing less 
Your function works as magic (even though the eval method is more simple to
implement)

I created a hash, hash keys, and called it with these lines:

my @outputFields1=sort @outputFields;
$insert->insert(\@outputFields1,\%outputArgs);
 
(just had to remove those “`”  from your code, they bring complains ) 

But - Is there a way to know that prepare() doesn’t work more than once for
each table type during the run ?




Wagner, Chris (GEAE, CBTS) wrote:
> 
> 
> 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);
> }
> 
> 

-- 
View this message in context: http://www.nabble.com/how-to-allow-variable-number-of-argumets-in-mysql-insert-query-tp14399793p14423828.html
Sent from the MySQL - Perl mailing list archive at Nabble.com.


-- 
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe:    http://lists.mysql.com/[email protected]