in reply to Re: Inserting CSV Into DB w/o CSV Modules
in thread Inserting CSV Into DB w/o CSV Modules
Here is what I came up with so far:
Thanks for all your help tachyon, your input is always very good.#!/usr/bin/perl use strict; use warnings; use DBI; use Text::CSV; my $sth; my $input="main_table.csv"; my $csv=Text::CSV->new(); open(INFILE,$input) || die "Can't open file $input"; ##Start database connections############### 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(<INFILE>) { # I'm not sure where to go from here, need to put CSV fields into vari +ables in SQL code? $sth->execute($state,$city,$location) or die "Couldn't execute query: +$dbh->errstr"; } $sth->finish; $dbh->disconnect; close(INFILE);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Inserting CSV Into DB w/o CSV Modules
by tachyon (Chancellor) on Nov 24, 2004 at 00:21 UTC |