Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: What is the better way to store and display data.

by Anonymous Monk
on Aug 21, 2012 at 16:22 UTC ( #988789=note: print w/ replies, xml ) Need Help??


in reply to Re^2: What is the better way to store and display data.
in thread What is the better way to store and display data.

I like this:
but;

#!/usr/bin/perl use strict; use warnings; use DBI; # Connect to the database, (the directory containing our csv file( +s)) my $dbh = DBI->connect("DBI:CSV:f_dir=.;csv_eol=\n;"); # Associate our csv file with the table name 'prospects' and # manually declare names for each of the columns $dbh->{'csv_tables'}->{'prospects.csv'} = { 'col_names' => ["name", "address", "floors", "donated", "c +ontact"] }; # Output the name and number of floors using our column names my $sth = $dbh->prepare("SELECT * FROM prospects.csv WHERE name LI +KE 'G%'"); $sth->execute(); while (my $row = $sth->fetchrow_hashref) { print("name = ", $row->{'name'}, ", Number of floors = ", $row->{'floors'}, "\n"); } $sth->finish();

This code is from "http://perlmeme.org/tutorials/parsing_csv.html". But running it gives this:
DBD::CSV::st execute failed: Error 2034 while reading file /media/Micr +oSD/code/prospects.csv: EIF - Loose unescaped quote at /usr/local/sha +re/perl/5.14.2/SQL/Statement.pm line 1055 [for Statement "SELECT * FROM prospects.csv WHERE name LIKE 'G%'"] at + propects.pl line 20. DBD::CSV::st fetchrow_hashref failed: Attempt to fetch row without a p +receeding execute () call or from a non-SELECT statement [for Stateme +nt "SELECT * FROM prospects.csv WHERE name LIKE 'G%'"] at propects.pl + line 21.


Comment on Re^3: What is the better way to store and display data.
Select or Download Code
Re^4: What is the better way to store and display data.
by Anonymous Monk on Aug 21, 2012 at 16:35 UTC
    Watch for leading spaces in your file.
Re^4: What is the better way to store and display data.
by Tux (Monsignor) on Aug 26, 2012 at 11:04 UTC

    Try playing with different ways to connect. Your CSV does not comply to minimal CSV rules, so you'd have to loosen them or complain to the author/generator of the CSV file.

    my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => ".", # Not needed, is default f_ext => ".csv/r", # advisable f_encoding => "utf-8", # probably needed csv_eol => "\n", # NOT needed! csv_allow_loose_quotes => 1, # You /might/ need this csv_allow_whitespace => 1, # OR this RaiseError => 1, # Always a good option! PrintError => 1, # Also a good choice });

    Look into the Text::CSV_XS documentation for what other csv_ options are allowed.

    FWIW, I will release a new DBD::CSV later today.


    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-11-23 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (131 votes), past polls