Solved

Insert data into db

Posted on 2004-08-12
7
189 Views
Last Modified: 2010-03-05
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
Comment
Question by:TPoly
  • 4
  • 3
7 Comments
 

Author Comment

by:TPoly
ID: 11790529
thanks...
0
 
LVL 18

Expert Comment

by:kandura
ID: 11793015
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
 

Author Comment

by:TPoly
ID: 11806849
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 18

Expert Comment

by:kandura
ID: 11808502
What error do you get?
What exactly don't you understand?
0
 

Author Comment

by:TPoly
ID: 11817011
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
 
LVL 18

Accepted Solution

by:
kandura earned 500 total points
ID: 11818911
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
 

Author Comment

by:TPoly
ID: 11826851
thanks.... it works :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

12 Experts available now in Live!

Get 1:1 Help Now