Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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


  }
 }
},".");
0
TPoly
Asked:
TPoly
  • 4
  • 3
1 Solution
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
kanduraCommented:
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.... it works :)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now