Andrew Kennard wrote:
> Hi all
> 
> Done lots of googling but this simple thing still has me stumped
> 
> The perl code to insert stuff into an mysql table is this
> 
>     my $Vals;
>     for ( my $i=1;$i<=32;$i++ ) {
>         $Vals.='?,';
>     }

Hmm, that seems like it would give you an extra comma at the end. BTW, you
could shorten that loop to:

   $Vals .= '?,' for 1..32;

I would use something like this to get placeholders for each value in
@TheRecord:

   my $Vals = join ',', ('?') x @TheRecord;

(obviously that would need to go after the line that populates @TheRecord).

>     chop $Vals;

Oh, I see where the extra comma is being removed. Clunky code, use my cool
stuff instead :~)

>     my @TheRecord=&GenMainRecData();
>     my $sth = $dbh->prepare("INSERT INTO mytable VALUES($Vals)");
>     $sth->execute(@TheRecord);
> 
> This works fine until I have single quores (') in the data eg
> O'Connor etc 
> 
> The data saved in the field ends up being OConnor
> 
> I've tried putting \\\' in the string but this gives me O\Connor

What you're doing should be working. The execute method should properly
quote your parameters.

You might try setting DBI->trace(2) prior to the call to execute() to get
some debugging output. Maybe that will help to pinpoint the problem.

> 
> I've also tried
>     foreach $fld (@TheRecord){
>         push (@Processed,$dbh->quote($fld));
>     }

No, you shouldn't have to do that. Your first approach is correct, so we
need to find out what's going wrong there...

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to