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

Hi everyone. I'm pretty new to Perl and everything concerned and I have a problem. I have a script that should select some values from a mysql table and copy them to another table (in another db). Then it should update two fields in the first table, so that already copied values aren't copied anymore. But I am not at all able to update them. Plus, are you aware of a way in which I could select data to be copied randomly instead than sequentially?<\br> Here's my code: <\br>
#!/usr/bin/perl #use strict; use diagnostics; ###################################################################### +######################## # si ricercano all'interno del database originario tutti gli alias (r +edirect) di ogni entrata. # Questo file prende in input 3_type_extracted.txt e il database (itwi +ki3) ricercando gli alias # all'interno della tabella redirect. Il file generato è 4_redirect_ty +pe_extracted.txt ###################################################################### +########################## BEGIN { use Exporter (); use vars qw(@ISA @EXPORT @EXPORT_OK); @ISA = qw(Exporter); @EXPORT = qw(); } use vars @EXPORT; use vars @EXPORT_OK; use vars qw(); $|++; use DB_File; use DBI; use Time::HiRes qw( usleep ualarm gettimeofday tv_interval ); use Term::ANSIColor qw(:constants); use Time::ParseDate; use Time::CTime; use HTML::Entities; use HTML::Parse; use Encode qw/is_utf8 decode encode_utf8 _utf8_off/; use Unicode::MapUTF8 qw(to_utf8 from_utf8 utf8_supported_charset); use POSIX qw(locale_h); use frameinit; setlocale(LC_ALL, "en_US.iso885915"); $PATHDATA = $frameinit::PATHDATA; $PATHSCRIPTS = $frameinit::PATHSCRIPTS; $debug = 0; $mysqldbpre= $frameinit::MYSQLDBPRE; $mysqldbins=$frameinit::MYSQLDBINS; $mysqldb = $frameinit::MYSQLDB; $mysqluser = $frameinit::MYSQLUSER; $mysqlpwd = $frameinit::MYSQLPWD; $myhost = $frameinit::MYHOST; ############################################# ##Mi collego al primo db ed estraggo i dati## ############################################# $TABLESOURCE = 'ar_web'; $dsn = "DBI:mysql:database=$mysqldbpre;host=$myhost"; $dbh = DBI->connect($dsn, $mysqluser, $mysqlpwd)|| print "NON RIESCO A + CONNETTERMI\n"; my($idc,$query,$dsub); my $range = 40; #Calcolo di un numero random da usare per la selezione dei campi# my $random_number = int(rand($range)); $date = `/bin/date`; $query = "SELECT idc, query, dsub3 FROM $TABLESOURCE where (assigned I +S NULL and evaluated IS NULL and dsub3 not like '') limit 100"; $sth = $dbh->prepare($query); $sth->{"mysql_use_result"} = 1; $sth->execute(); ($idc,$query,$dsub3) = $sth->fetchrow_array or warn "Error in fetch (P +age_id:$page_id): $DBI::errstr\n"; ########################################### ##Aggiorno i dati nella tabella INSTANCE## ########################################### print ">>> crea tabella instance, db $mysqldbins with script in $P +ATHSCRIPTS ... "; $rc = system("cd $PATHSCRIPTS; ./crea_instance.sh"); print "($rc) OK\n"; #print LOG ">>> crea tabella instance, db $mysqldbins => $rc\n"; $TABLEINST = 'instance'; $dsg = "DBI:mysql:database=$mysqldbins;host=$myhost"; $dbk = DBI->connect($dsg, $mysqluser, $mysqlpwd)|| print "NON RIESCO A + CONNETTERMI\n"; while($idc){ $idao = $idc.'_'.$TABLESOURCE; $insert = "insert into $TABLEINST (idao,oggetto,valore) values + (?,?,?)"; $sthy = $dbk->prepare($insert); if(!defined($sthy)){ #print RED,"Error: $DBI::errstr\n$select\n",RESET; } $rvy = $sthy->execute($idao, $query, $dsub3); if(!defined($rvy)){ #print RED,"Error: $DBI::errstr\n$select\n",RESET; } ($idc,$query,$dsub3) = $sth->fetchrow_array } ############################################################## ##Aggiorno i campi Evaluated e Assigned della tabella source## ############################################################## $dsq = "DBI:mysql:database=$mysqldbpre;host=$myhost"; $dbq = DBI->connect($dsq, $mysqluser, $mysqlpwd)|| print "NON RIESCO A + CONNETTERMI\n"; print ">>> update tabella $TABLESOURCE, db $mysqldbpre with script + in $PATHSCRIPTS ... "; while($idc){ $update = "UPDATE $TABLESOURCE SET assigned='1', evaluated='1' + WHERE (idc LIKE '$idc')"; $stq = $dbq->prepare($update); $rvq = $stq->execute($assigned, $evaluated); ($idc,$query,$dsub3) = $sth->fetchrow_array } print "OK\n"; ###################################################################### +########
Thanks to anyone willing to help. Suggestions and criticism are very much appreciated.

Replies are listed 'Best First'.
Re: Mysql update table problem
by samarzone (Pilgrim) on Nov 09, 2010 at 14:10 UTC

    Haven't gone through your code but here are a few tips

    1. Use DBI error messages to understand the problems, e.g. $sth = $dbh->prepare($query) || die $DBI::errstr or $sth->execute() or die $DBI::errstr
    2. Use debugger "perl -d"
    3. If you are not comfortable with debugger sometimes you can even use print statements to check the progress. Error in "prepare" will result in undefined statement handle
    4. And finally after you have done all above, if you are still not able to understand the problem try to put only part of the code where you actually see the problem

    I hope the very first option will solve your problems

      I still can't find where the problem lies. I select the right fields with my first query, I copy the data into the right table with the second query. What I need to do then, is update the first table (using two specific columns), so that the data that are already used won't be used no more. For every selected data, I have to update the column "assigned" and the column "evaluated" with a number, obviously just for data that are selcted by the first query. I don't know how to do it. I tried what you suggested but with no success.

        Check for the return value of "execute". If transaction is successful it should return the number of affected rows. Otherwise it will set the variable $DBI::errstr.

        Also have a look at "RaiseError"

        Do post what you have done and what you got.