On Sun, Nov 7, 2010 at 8:13 PM, shawn wilson <ag4ve...@gmail.com> wrote:

> Use strict and warnings. Probably should also use parentheses:
> open(CSV, "< $file")
> On Nov 7, 2010 9:01 PM, "J M" <jmd...@gmail.com> wrote:
> > Hi all,
> >
> > I'm having great issues with my program at the moment... it is supposed
> to
> > take a csv file and parse it, with the output being stored in a MySQL
> > database. The problem I'm having is that the file I need to parse
> > ('ish_history.csv') will not open in the program for some reason. I know
> the
> > file exists, I'm on a linux system and the file is world-readable, and
> I've
> > tried placing the file in the directory the script is run from AS WELL as
> > using full path names to said file... all has resulted in no luck, and
> I'm
> > getting the "No such file or directory" error. Here is my code so far:
> >
> >
> >
> > [code]
> > #! /usr/bin/perl
> > #
> > use DBI;
> > use DBD::mysql;
> > use Text::CSV;
> >
> >
> > #configure variables
> > my $host = "localhost";
> > my $db = "somefakedatabase";
> > my $table = "somefaketable";
> > my $user = "somefakeuser";
> > my $password = "yeahrightlikethisismyrealpasswordLOL";
> >
> > #connect to the database
> > my $dbc = "dbi:mysql:$db:$host";
> > if(my $dbh = DBI->connect($dbc, $user, $password) or die "Can't connect
> to
> > the
> > database: $DBI::errstr\n") {
> > print "Connected to the database!";
> > }
> >
> > #setup parser
> > my $csv = Text::CSV->new();
> > print "What is the name of the file? : ";
> > chomp(my $file = <STDIN>);
> >
> > open CSV, "<", $file or die "Can't open the file \"$file\": $!";
> > while(<CSV>) {
> > next if ($. == 1);
> > if ($csv->parse($_)) {
> > my @columns = $csv->fields();
> > my $myquery = $dbh->prepare('insert into $table
> >
> (station_number,WBAN_number,station_name,country,fips,state,ICAO_callsign,lattitude,longitude,elevation)
> > values ($columns[0], $columns[1], **truncated**
> > $myquery->execute();
> > $dbh->disconnect();
> > } else {
> > my $err = $csv->error_input;
> > print "Failed to parse line: $err";
> > }
> > }
> > close CSV;
> > closedir TMPDIR;
> > [\code]
> >
> > Any ideas as to why this isn't working?
>

I should've mentioned it, but I did both of those things as well.... in
fact, those were in the first version of the program and I thought maybe
taking THEM out would help lol... thank you though!

Reply via email to