The first question would be: "Does the CSV have a header line, or can it be made to have a header line?". If so, your initialisation could be much simpler. And the ~ does not have to be escaped at all.

$ cat data/objectives-cqdc.txt Obj~PosNbr~Year~Div~AVP~Dept~SDescr~Major~Methods~Results~Accompl~JanU +p~LDescr~Changes~Chars~Goals~Budget~CFObj 20140000020~00001576~2014~"testdata"~"testdata"~"testdata"~-~0~-~"Inse +rted on December 10, 2013"~-~-~-~-~20000000001~20130000001~1~- $ cat test.pl use 5.16.2; use warnings; use DBI; use Data::Peek; my ($Cposition, $Cyear) = ("00001576", 2014); my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => "data", f_ext => ".txt/r", csv_sep_char => "~", }); my $sth = $dbh->prepare (qq{select * from "objectives-cqdc" where PosN +br = ? and year = ?}); $sth->execute ($Cposition, $Cyear); while (my $row = $sth->fetchrow_hashref) { DDumper $row; } $ perl test.pl { accompl => '-', avp => 'testdata', budget => 1, cfobj => '- ', changes => '-', chars => '20000000001', dept => 'testdata', div => 'testdata', goals => '20130000001', janup => '-', ldescr => '-', major => 0, methods => '-', obj => '20140000020', posnbr => '00001576', results => 'Inserted on December 10, 2013', sdescr => '-', year => 2014 } $

Also note that file has been deprecated in favor of f_file to be more consistent with DBD::File specifications. An aditional disadvantage of using specific csv_tables initialisation is that the table name in not case insensitive anymore and that the initialisation takes MUCH longer, as DBD::CSV will scan the default f_dir (defaults to ".") for usable table names possible taking a lot of IO you do not want. Your new code when not having a header could look like this:

use DBI; use Data::Peek; my ($Cposition, $Cyear) = ("00001576", 2014); my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { csv_sep_char => "~", csv_tables => { JPDObj => { f_file => "data/objectives-cqdc.txt", col_names => [qw( Obj PosNbr Year Div AVP Dept SDescr Major Methods Resu +lts Accompl JanUp LDescr Changes Chars Goals Budget CFObj +)], }, } }); my $sth = $dbh->prepare (qq{select * from JPDObj where PosNbr = ? and +year = ?}); $sth->execute ($Cposition, $Cyear); while (my $row = $sth->fetchrow_hashref) { DDumper $row; }

Enjoy, Have FUN! H.Merijn

In reply to Re: DBD::CSV and fetchrow_hashref not working by Tux
in thread DBD::CSV and fetchrow_hashref not working by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.