Solved

Insert data into db

Posted on 2004-08-12
7
212 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

11 Experts available now in Live!

Get 1:1 Help Now