If you can identify a "primary key" value in the data that you're putting into the table -- e.g. if the combination of "round" and "playername" should never repeat in the table -- then you could start with a query that pulls out all the existing "round,playername" tuples in the table, and store those as keys to a hash (values assigned to those keys won't matter, just so long as the keys are made to exist).

Then, use the "prepare" method in DBI to create two SQL statement handles -- one for update, and one for insert.

Now, as you go through the "weekly_file", check each "round,playername" value to see if it exists as a hash key from that initial query -- if it's there, use the update statement handle, otherwise, use the insert.

I wrote a wrapper for DBI that makes this sort of thing fairly simple to code in Perl (the pod even gives an example a bit like this) -- I'm not claiming it's the "best" or "optimal" solution, but it works for me, and saves me a lot of time when writing Perl code for SQL operations like this.


In reply to Re: checking to do a insert or update by graff
in thread checking to do a insert or update by perlinacan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.