Solved

Repeated data inserted into db

Posted on 2004-08-13
4
198 Views
Last Modified: 2010-03-05
hi ..
i have a perl script that extract the specific keywords from text files .

the text files extracted each time is stored in the computer approx 1 year
and whenever a new file come in, the keywords is being inserted into the db

Now, i don't want repeated keywords from the files being inserted into the db each time there's a new file coming in.

i use crontab to run my FTP script(for transfering of files) and perl script (for inserting keywords into db).
I also using mysql as my db and debian linux 3.0 as OS

this is my perl script

Perl Script

use DBI;
use File::Spec::Functions qw(curdir rel2abs);
use File::Find;
 my( $dbh, $sth, $query );

 $dbh = DBI->connect( 'dbi:mysql:rvsi');
 $query = 'insert into yield ( job, date, tray, lot, dir, report ) values ( ?, ?, ?, ?, ?, ?)';
 $sth = $dbh->prepare( $query );

find(
sub{
 return unless -f;
   
 $fname = rel2abs( $_ );
 print "$fname\n";

 @ARGV = ($_);
 my ( $job, $tray, $date, $lot, $report);

 while(<>) {
  if( /^\s*(\S+)\s*Report/ ){
    $report ="$1 report";
    print $report;
  } elsif( /Job\s*:\s*(\S+)\s*Start Date\s*:\s*(\S+)/ ) {
    $job = $1;
    $date = $2;
    print "$job\n";
    print "$date\n";
  } elsif( /Tray\s*:\s*(\S+)/ ) {
    $tray = $1;
    print "$tray\n";
  } elsif( /Lot\s*:\s*(\S+)/ ) {
    $lot = $1;
    print "$lot\n";

  }
 

if( $job && $date && $tray && $lot && $report ){
    # We have a full record
   
    my $fname = rel2abs( $ARGV);
    $sth->execute($job, $date, $tray, $lot, $fname, $report);

    $job = $date = $tray = $lot =  '';


  }
 }
},".");


Thanks.. :>

0
Comment
Question by:TPoly
  • 2
4 Comments
 

Assisted Solution

by:fukengruven
fukengruven earned 100 total points
ID: 11791596
I'm not sure if this is the best way, but it would depend on the number of keywords. It sounds like you're going to have to perform some comparisons to see if they are already present in the database.

If you have an obscene amount of keywords then you might consider organizing the database differently (maybe seperated alphabetically) to keep from suffering a performance hit.

I would read in the words from the file and arrange them alphabetically in an array and then iterate thru the array and compare them to the query results I got from the database. Like I said, I would probably redesign the database so you wouldn't have to iterate thru the entire database every time.
0
 
LVL 8

Accepted Solution

by:
davorg earned 400 total points
ID: 11791721
So in some cases you can do an update instead of an insert.

In fact, as you're using MySQL, you can use "replace" and it will do an insert or an update as appropriate. You just need to ensure that that you have the right primary keys on the table.
0
 

Author Comment

by:TPoly
ID: 11806824
hi... i've tried the "replace"  
it still doesnt work
my primary key is the auto-increment serial number
is that consider as the correct primary key?
0
 

Author Comment

by:TPoly
ID: 11807523
yea ok i found out the problem
it's my PK

thanks for ya help.... it works :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now