in reply to error calling MySQL from Perl

Apart from the syntax problem, MySQL versions earlier than 4.1 (which is far from stable at the moment) do not support subqueries.

Here's a workaround to such limitation.

#!/usr/bin/perl -w use strict; use DBI; my ($dbh, $rows); $dbh = DBI->connect('dbi:mysql:test', 'myusername', 'mypassword', {RaiseError=>1}) || die "Error opening database: $DBI::errstr\n"; # # First, get destination from your table # my ($destination) = @{$dbh->selectcol_arrayref(qq{ SELECT destination FROM checkin WHERE firstname='Henry' AND lastname='Rollins'} )} # || die "destination not found\n"; # wrong or die "destination not found\n"; # correct # # Notice that the script fails if destination is not found. # # # then, use $destination to update the table # $rows = $dbh->do(qq{UPDATE checkin SET destination = '$destination' WHERE firstname='Bill' AND lastname='Gates'}) || die "Couldn't update record : $DBI::errstr"; print "$rows row(s) updated in checkin"; $dbh->disconnect || die "Failed to disconnect\n";

A few comments:

update - Fixed a small bug in the script.

_ _ _ _ (_|| | |(_|>< _|

Replies are listed 'Best First'.
Re: Re: error calling MySQL from Perl
by mooseboy (Pilgrim) on Mar 16, 2003 at 20:23 UTC

    Thanks for the workaround and the tips -- I tested it but now Bill is flying off to the mysterious destination "1"?! I only installed MySQL yesterday and got so engrossed I forgot to take the most elementary security precautions. Duh!

    Update: tested gmax's code again incorporating small bugfix -- now works perfectly! Looks like I'll have to fork out for a copy of O'Reilly's Programming the Perl DBI. Cheers to all monks for their helpful suggestions.