Solved

Perl CSV to Access- IF RECORD DOESNT EXIST

Posted on 2010-11-20
1
542 Views
Last Modified: 2012-05-10
The code below works correctly and inserts values from the CSV file into the Access Database. How do I adjust the script to check and see if the record from the CSV file already exists in the database?

I do not want to import duplicate records. Thanks.
use DBI;

$fhour="00";
$count=0;
$finterval=12;

do {

$csvfile="C:/WeatherModel/degrib/bin/Model/Output/gfs_2010111900_" . $fhour. ".csv";

$dbh = DBI->connect('dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=c:/weathermodel/weathermodel.mdb', '', '');
$sth = $dbh->prepare( "INSERT INTO GFS_Forecasts VALUES (?,?,?,?,?,?)" );

open CSV, $csvfile;


$x=0;
while(<CSV>) {


if ($x>0){
	  chomp;
  my @fields = split /,/;	

  $sth->execute(@fields);
}
$x++;

}

Open in new window

0
Comment
Question by:kyle972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 34181242
I think this will do what you want.  I know Access has some eccentricities so I'm not positive this will work - if not, let me know and I'll work around whatever the error is.

You will have to change the fieldX names in the sql after the comment line with XXX to match what is in the database.
use strict;
use warnings;
use DBI;

my $fhour="00";
my $count=0;
my $finterval=12;

# code seems to be incomplete - this do is never closed
# do {

my $csvfile="C:/WeatherModel/degrib/bin/Model/Output/gfs_2010111900_" . $fhour. ".csv";

my $dbh = DBI->connect('dbi:ODBC:driver=Microsoft Access Driver (*.mdb);dbq=c:/weathermodel/weathermodel.mdb', '', '');
my $sth = $dbh->prepare( "INSERT INTO GFS_Forecasts VALUES (?,?,?,?,?,?)" );
# XXX - need to change field names to match database
my $sth2 = $dbh->prepare("select count(*) from GFS_Forecasts where field1 = ? and field2 = ? and field3 = ? and field4 = ? and field5 = ? and field6 = ?");

open CSV, $csvfile;

$x=0;
while(<CSV>) {
    if ($x>0){
        chomp;
        my @fields = split /,/;       
        $sth2->execute(@fields);
        my $cnt = $sth2->fetchrow_arrayref()->[0];
        $sth->execute(@fields) unless $cnt;
    }
    $x++;
}

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

688 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