It's been ages since I've written direct database queries, but something like the following could be what you use.

Using prepared statements and placeholders are the biggest things I'd recommend:

#!/usr/bin/perl use strict; use warnings; use DBI; use DBD::mysql; #this is my config module use DBase::MySQL::Config qw(DB_CUSTOMER_CATALOG DB_USER DB_PW); my $dbh = DBI->connect(DB_CUSTOMER_CATALOG, DB_USER, DB_PW); my $catalog_u = $dbh->prepare(q{UPDATE catalog SET name=?, address=?, +notes=? WHERE phone_number=?}); my $catalog_i = $dbh->prepare(q{INSERT INTO catalog SET phone_number=? +, name=?, address=?, notes=?}); my $catalog_c = $dbh->prepare(q{SELECT COUNT(*) FROM catalog WHERE pho +ne_number=?}); my $change_i = $dbh->prepare(q{INSERT INTO change_log SET timestamp=NO +W(), number_record=?, action=?}); while(<DATA>) { chomp; my ($phone, $name, $address, $notes) = split "\t"; my $catalog_c->execute($phone) or die $dbh->errstr; my ($exists) = $catalog_c->fetchrow_array; if ($exists) { my $changed = $catalog_u->execute($name, $address, $notes, $ph +one) or die $dbh->errstr; if ($changed) { $change_i->execute($phone, 'update') or die $dbh->errstr; } } else { $catalog_i->execute($phone, $name, $address, $notes) or die $d +bh->errstr; $change_i->execute($phone, 'insert') or die $dbh->errstr; } } $catalog_u->finish; $catalog_i->finish; $catalog_c->finish; $change_i->finish; $dbh->disconnect(); __END__

In reply to Re: Using Perl and MySQL for Activity Log by wind
in thread Using Perl and MySQL for Activity Log by PyrexKidd

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.