in reply to DBD::CSV and fetchrow_hashref not working

Well I ran your code on the sample input and it works, provided $Cposition is '00001576' and $Cyear is '2014'
while (my $row = $sth->fetchrow_hashref) { for my $k (keys %$row) { print qq|$k: $row->{$k}\n|; } } # Output # Year: 2014 # Major: 0 # SDescr: - # LDescr: - # Obj: 20140000020
At first I thought it was the csv_sep_char=\\~ that was the problem and that should be csv_sep_char=\~ but it works with either for some strange reason.

It could simply be that you don't actually do anything to inform you of the results - did you remove the code from the line where it says: # some output code here?

Replies are listed 'Best First'.
Re^2: DBD::CSV and fetchrow_hashref not working
by Anonymous Monk on Dec 11, 2013 at 20:45 UTC

    Thank you for your reply. I still cannot get it to work despite trying the code. Perhaps it is a server setting. It's like it doesn't fetch at all.

    And, yes, in the full code, I am actually doing something to output the results.

    -Jacvivs

      Maybe you should test the connection and other workings first, something like:
      my $dbh = DBI->connect("dbi:CSV:csv_eol=\n;csv_sep_char=\~") or die $D +BI::errstr; $dbh->{'RaiseError'} = 1; $@ = ''; eval { $dbh->{'csv_tables'}->{'JPDObj'} = {'file' => 'data/objectives-cqd +c.txt','col_names' => ["Obj","PosNbr","Year","Div","AVP","Dept","SDes +cr","Major","Methods","Results","Accompl","JanUp","LDescr","Changes", +"Chars","Goals","Budget","CFObj"]}; my $query = "SELECT * FROM JPDObj"; my $sth = $dbh->prepare($query); $sth->execute(); while (my $row = $sth->fetchrow_hashref) { for my $k (keys %$row) { print qq|$k: $row->{$k}\n|; } } }; $@ and die "SQL database error: $@";
Re^2: DBD::CSV and fetchrow_hashref not working
by Anonymous Monk on Dec 11, 2013 at 21:18 UTC

    It ended up being bad data in some of the other records that were manually put in the file. I found that I actually had to remove the quotes from the eol specification in the dbh line and it worked.

    Here's my code for the dbh line:

    my $dbh = DBI->connect(qq{DBI:CSV:csv_eol=\n;csv_sep_char=\\~}); $dbh->{'csv_tables'}->{'JPDObj'} = {'file' => 'data/objectives-cqdc.tx +t','col_names' => ["Obj","PosNbr","Year","Div","AVP","Dept","SDescr", +"Major","Methods","Results","Accompl","JanUp","LDescr","Changes","Cha +rs","Goals","Budget","CFObj"]};

    -Jacvivus