Solved

Perl CSV to Access- IF RECORD DOESNT EXIST

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
update query 4 28
Gracefully handling 'Record Locked'  Errors 33 38
Record Lock on Database will not go away 9 25
Access - Rounding an average in a report 3 16
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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