bory has asked for the wisdom of the Perl Monks concerning the following question:

Hello great perlmonks! I have a mysql database and i want to update some records from it, but instead of updating only those records it rewrites the whole database with the records updated and i lose the rest of the information. Here is my code:
my @name=("Anna","Maria","Carmen"); my @age=("25","27","29"); my @eyes=("brown","blue","pink"); #those arrays have always the same number of elements for (my $i=0;$i<=$#name;$i++){ my $sth=$dbh->prepare(qq{UPDATE tnt SET `Age`=\"$age[$i]\",`Ey +es`=\"$eyes[$i]\" WHERE `Name`=\"$name[$i]\"}); $sth->execute(); } $sth->finish;
If i have in the database the following entries:
Name Age Eyes Corina 13 red Ada 17 blue Anna 10 grey Maria 2 white Carmen 6
After updating in my way:
Anna 25 brown Maria 27 blue Carmen 29 pink
and i lose the other records. Thank you very much for your time

Replies are listed 'Best First'.
Re: updating a database
by Abigail-II (Bishop) on Feb 10, 2004 at 10:28 UTC
    That sounds pretty hard to believe - and if it happens, it might as well be a MySQL error than a Perl issue. But my guess is that you either erase the other data somewhere else in your program, or that you start out with different data than you expect.

    I would also recommend you use placeholders. Not only will that make your code faster (you need just one prepare), you also have less problems with quoting. And what's the deal with backwacking the double quotes? And why put the column names in backticks? Why does the loop run till $i equals $#test, when the code doesn't show @test? Why don't you check the return values of the prepare and the execute?

    Abigail

Re: updating a database
by borisz (Canon) on Feb 10, 2004 at 09:44 UTC
    I think the error is not in the update code.
    Boris