#!/usr/bin/perl

use strict;
use DBI;

  my $dbh = DBI->connect ("DBI:mysql:trap:localhost","trap","april17")
             || die "Could not connect to database: "
             . DBI-> errstr;

   my $sql_stmnt = "SELECT id,PathToFile FROM wafpng";
   my $sth = $dbh->prepare($sql_stmnt);
   $sth->execute();
   # Retrieve results and reference number
   my $plots = $sth->fetchall_arrayref();
   
   $sth->finish();

      foreach my $path (@$plots){
       my ( $id, $PathToFile ) = @$path;

	#print "$id $PathToFile\n";
	my $test = $dbh->do("SELECT id,PathToFile FROM wafpng WHERE  id = '$id' AND plot=''");
	if ($test != "1")
	{
	#print "Exists!\n";
	}
        if ($test == "1")
	{
	#print "Import plot $id $PathToFile\n";
	my $fileName="/home/trap/wafer_test_logs_v2/". $PathToFile."adc_dds.png";
	open( FILE, "<$fileName");
	my @file = join("",<FILE>);
	close FILE;
	my $fileStr = join("",@file);
	my $sth = $dbh->do("UPDATE wafpng SET plot=". $dbh->quote($fileStr) ." WHERE id=$id");
	}

}


$dbh->disconnect();

