Solved

Insert data into db

Posted on 2004-08-12
7
223 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…

856 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