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

fellow monks, something's bothering me these days.
what i have:
  • a csv-file (put into an array)
  • a mysql-table (fetched all into hash_ref)

    what i want to do:
  • update elements b and c of the csv-array with values of the mysql-hash_ref where element a of the csv-array matches the key of the mysql-hash_ref.
  • if element a does not match, update element b and c where element b of the csv-array matches the key of my_other_mysql-hash_ref have a look at the code:
    my $sth = $dbh->prepare("SELECT PlzOrt, Ortsbezeichnung, PlzGemein +de, GemeindeId FROM ortzugemeinde"); $sth->execute; my $hash_ref_ort = $sth->fetchall_hashref('Ortsbezeichnung'); $sth->execute; my $hash_ref_plz = $sth->fetchall_hashref('PlzOrt'); $sth = $dbh->prepare( q{ INSERT INTO clk_clicks (clk_year, clk_month, obj_id, own_id, cl +k_percent, clk_days, clk_plz, gde_name, clk_price, prc_id, obt_id, cl +k_total, clk_object, clk_detail, clk_pdf, clk_direct, clk_vmail, clk_ +fmail, gde_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? +, ?, ?,?) }); my @content; my @table; open FH, "< mydir/ImportFile_200201.txt" or die "Unable to open fi +le: $!"; while (<FH>) { chomp; @content = split /;/; if (exists $hash_ref_ort->{'$content[7]'}) { $content[18] = $hash_ref_ort->{'$content[7]'}->{'GemeindeI +d'}; $content[6] = $hash_ref_ort->{'$content[7]'}->{'PlzGemeind +e'}; } elsif (exists $hash_ref_plz->{$content[6]}) { $content[18] = $hash_ref_plz->{$content[6]}->{'GemeindeId' +}; $content[6] = $hash_ref_plz->{$content[6]}->{'PlzGemeinde' +}; } else { $content[18] = 0; } $sth->execute(@content); push @table, [@content]; } close FH; $dbh->commit; $dbh->disconnect; print "ok<br>";

    one line of the csv-file looks like:
    2002;01;62;6;1;31;5034;Suhr;3;435000;5;14;13;1;0;0;0;0

    this means: i want to append another value at EOL if 'Suhr' exists in $hash_ref_ort. if not i want to append another value at EOL if '5034' exists in $hash_ref_plz. if not, i want to append a 0 at EOL.

    note: i haven't fully optimized the code yet.

    it's basically something like this:
    UPDATE table1 INNER JOIN table 2 ON table1.ort = table2.ortsbezeichnung
    SET table1.gde_id = table2.gde_id
    WHERE table2.ortsbezeichnung = $ort;

    (but it seems that mysql does not support multi-table-updates, does it. )

    now what bothers me:
    this code behaves strange: usually it inserts 18 records into the db and gets stuck. some few times it inserted all the records into the db. some other times it does not do anything. i tried to uncomment line after line but this does not really change anything. and, oh yes, i tried it on another machine too.
    the thing is running (should i say 'walking'?) on win2k with apache 1.3.26.

    thank you all for your comments. i've run out of ideas to solve the problem since i do not have any ideas where to start to solve it.

    update: i'm wondering: the keys in the hash are i.e.
    {'Erlinsbach'}
    {'Niedererlinsbach'}
    as far as i know they should be unique anyway? even though they do contain the same letters? otherwise i'd be consternated...

    update II:
    turn out it's not a problem with the code. since i thought it's the code i posted here...
    i'm not sure WHAT it is, but i tried it (the same script) on my MacOSX-powerbook and it runs smothly.
    since the server i developed it, isn't a real server but a client (as my HW-geek told me today), my thoughts go into overflow of maximum connections.

    i'm just happy it's not my code... :) thanks anyway...
  • Replies are listed 'Best First'.
    Re: updating array with matching value of hash
    by Roy Johnson (Monsignor) on May 06, 2004 at 14:26 UTC
      If they keys compare ne, they will be different elements of a hash.

      I cannot explain why your database would be hanging up after some arbitrary number of inserts.


      The PerlMonk tr/// Advocate