Solved

Perl CSV to Access- IF RECORD DOESNT EXIST

Posted on 2010-11-20
1
538 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
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

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…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

744 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