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

fungazid <[email protected]> Wed, 19 Dec 2007 05:15:57 -0800 (PST)
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
Thanks Chris,

Using prepare_cached() is a good idea, 
Does your function calls execute() with an array ? (I think mysql will
complain)
I found a simple that works perfectly for me, which is to use eval() !

$str='$args[1],$args[2],$args[3],$args[4],$args[5],';
my $str2='$q->execute('.$str.') or die "Couldn\'t execute statement: " .
$q->errstr'; 
eval $str2 or die "error!!!\n"; 




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);
> }
> 
> 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]
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/how-to-allow-variable-number-of-argumets-in-mysql-insert-query-tp14399793p14416766.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]