#!/usr/bin/perl use strict; use warnings; use DBI; use Tie::Handle::CSV; my $csv_fh = Tie::Handle::CSV->new('main_table.csv', header => 1); my $sth; my $database = "database"; my $db_server = "localhost"; my $user = "user"; my $password = "password"; my $dbh = DBI->connect("DBI:mysql:$database:$db_server", $user, $password); my $statement = "INSERT INTO table (state, city, location) VALUES (?,?,?)"; $sth = $dbh->prepare($statement) or die "Couldn't prepare the query: $sth->errstr"; while (my $csv_line = <$csv_fh>) { my $state = $csv_line->{'State'}; my $city = $csv_line->{'City'}; my $locationname = $csv_line->{'Location'}; my $rv = $sth->execute($state,$city,$locationname) or die "Couldn't execute query: $dbh->errstr"; } my $rc = $sth->finish; $rc = $dbh->disconnect; close $csv_fh;