Insert data into db

hi..
I have some text file but with  different heading eg "Yield report" and
"Tray report" and i have 2 database table called "yield" and "tray".is it possible
that the perl can be written to insert the keywords into its repective db?
If possible can provide me with the code??

Text File

   Yield Report
Job      : PQFP65       Start Date: 06/31/04
Tray     : Peak-15      Start Time: 14:40:35
Package  : PQFP-64      Sensor Ht : 239.17mils
Operator : CINDY        Run Time  : 16:11:53
Mach S/N : LSI318       Lot       : D4550028
---------------------------------------------------------------------------------

   Tray Report
Job      : PQFP65       Start Date: 06/31/04
Tray     : Peak-15      Start Time: 14:40:35
Package  : PQFP-64      Sensor Ht : 239.17mils
Operator : CINDY        Run Time  : 16:11:53
Mach S/N : LSI318       Lot       : D4550028

===================================================

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 =  '';


  }
 }
},".");
TPolyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
kanduraConnect With a Mentor Commented:
That's odd. If I try the same query, I get no error at all.
Oh wait! I guess the complete query looked something like

    insert into tray report (...)

and that is of course not right.
That happens because of these lines:
  if( /^\s*(\S+)\s*Report/ ){
    $report ="$1 report";               #### <-- here we assign 'Tray report' to $report, and I expected just 'Tray'
    print $report;

What you need is this:

 if($report =~ /(yield|tray)/i) {
        my $table = lc($1);
        $dbh->do( qq{insert into $table ( job, date, tray, lot, dir, report ) values ( ?, ?, ?, ?, ?, ?)}, undef, $job, $date, $tray, $lot, $fname, $report);
    } else {
        print "Got Report $table, which is not supported!$/";
    }

What this does is check whether report contains 'yield' or 'tray'. If it does, it is converted to lowercase and assigned to $table. Then the query is executed.

HTH,
Kandura
0
 
TPolyAuthor Commented:
thanks...
0
 
kanduraCommented:
currently your query always stores in the 'yield' table.

Here are some changes that should make the script do what you want:

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

 $dbh = DBI->connect( 'dbi:mysql:rvsi');

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);
    if($report =~ /yield|tray/i) {
        $report = lc($report);
        $dbh->do( qq{insert into $report ( job, date, tray, lot, dir, report ) values ( ?, ?, ?, ?, ?, ?)}, undef, $job, $date, $tray, $lot, $fname, $report);
    } else {
        print "Got Report $report, which is not supported!$/";
    }

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


  }
 }
},".");
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TPolyAuthor Commented:
hi,i tried but cannot insert into db

Sorry but can you explain this part for me?

 if($report =~ /yield|tray/i) {
        $report = lc($report);
        $dbh->do( qq{insert into $report ( job, date, tray, lot, dir, report ) values ( ?, ?, ?, ?, ?, ?)}, undef, $job, $date, $tray, $lot, $fname, $report);
    } else {
        print "Got Report $report, which is not supported!$/";
    }


Thanks :)
0
 
kanduraCommented:
What error do you get?
What exactly don't you understand?
0
 
TPolyAuthor Commented:
This is the error that i have:

DBD::mysql::db do failed: You have an error in your SQL syntax near 'report ( job, date, tray, lot, dir, report ) values ( 'PQFP30', '02/17/03', 'Pea' at line 1 at mysqlperl7.pl line 45, <> line 6.

This is part that i do not understand:

 if($report =~ /yield|tray/i) {
        $report = lc($report);
        $dbh->do( qq{insert into $report ( job, date, tray, lot, dir, report ) values ( ?, ?, ?, ?, ?, ?)}, undef, $job, $date, $tray, $lot, $fname, $report);
    } else {
        print "Got Report $report, which is not supported!$/";
    }

I think the error is at :
        $report = lc($report);
        $dbh->do( qq{insert into $report ( job, date, tray, lot, dir, report ) values ( ?, ?, ?, ?, ?, ?)}, undef, $job, $date, $tray, $lot, $fname, $report);

because for this part is able to be printed out when yield or tray is not found:
    } else {
        print "Got Report $report, which is not supported!$/";
    }

thanks
0
 
TPolyAuthor Commented:
thanks.... it works :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.