awohld has asked for the wisdom of the Perl Monks concerning the following question:
Can someone show me a generic or other way to load CSV files in my code that doesn't need Tie::Handle::CSV; or any other CSV module?
Thanks#!/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, $passw +ord); 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 ex +ecute query: $dbh->errstr"; } my $rc = $sth->finish; $rc = $dbh->disconnect; close $csv_fh;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Inserting CSV Into DB w/o CSV Modules
by tachyon (Chancellor) on Nov 23, 2004 at 04:50 UTC | |
by matija (Priest) on Nov 23, 2004 at 07:26 UTC | |
by awohld (Hermit) on Nov 23, 2004 at 16:32 UTC | |
by tachyon (Chancellor) on Nov 24, 2004 at 00:21 UTC | |
Re: Inserting CSV Into DB w/o CSV Modules
by davorg (Chancellor) on Nov 23, 2004 at 08:54 UTC | |
Re: Inserting CSV Into DB w/o CSV Modules
by hmerrill (Friar) on Nov 23, 2004 at 12:58 UTC | |
Re: Inserting CSV Into DB w/o CSV Modules
by jZed (Prior) on Nov 23, 2004 at 18:13 UTC |