How to read CSV files where the first line defines the field names? 
Author Message
 How to read CSV files where the first line defines the field names?

Says it all.

I'm wanting to read in CSV files via perl on a Unix system, and thought
DBI::CSV looked up to the job.

However, code as follows doesn't work (reading file test.csv):

use DBI;
$dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\,});
$dbh->{'csv_tables'}->{'test'} = { 'file' => 'csv'};
$sth = $dbh->prepare("SELECT Company FROM test");
$sth->execute();
$co = $sth->fetchrow_array;
print "$co\n";

The first line defines "Company" as being field number 8.

Am I missing something obvious here?

Perl-5.004,DBI-1.06,DBD::CSV.

--
Cheers

Jason Haar

Unix/Network Specialist, Trimble NZ
Phone: +64 3 3391 377 Fax: +64 3 3391 417



Sun, 26 Aug 2001 03:00:00 GMT  
 How to read CSV files where the first line defines the field names?
[emailed and posted]

Quote:

> I'm wanting to read in CSV files via perl on a Unix system, and thought
> DBI::CSV looked up to the job. [snip]
> use DBI;
> $dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\,});
> $dbh->{'csv_tables'}->{'test'} = { 'file' => 'csv'};
> $sth = $dbh->prepare("SELECT Company FROM test");
> $sth->execute();
> $co = $sth->fetchrow_array;
> print "$co\n";

If you want to read a standard CSV file with the first line being the
field names, commas as field separators, and newlines as record
separators, then you don't need any of the stuff about sep_char or the
line about csv_tables.  Also your driver string doesn't include a
directory name; you don't have any error trapping to know what is going
wrong; and you are treating the array of returned values as a scalar.

Try this instead, it will work on any proper CSV file.  All you have to
do is change the $dbDir variable to the name of the directory where the
file 'test' is located.

#!/usr/local/bin/perl -w
use strict;
use DBI;
my $dbDir = 'the/directory/location/of/your/test/CSV/file/here';
my $dbh = DBI->connect("DBI:CSV:f_dir=$dbDir") or die $DBI::errstr;
my $sth = $dbh->prepare("SELECT Company FROM test") or die $DBI::errstr;
$sth->execute()  or die $DBI::errstr;

    die $sth->errstr if $sth->err;

Quote:
}

$dbh->disconnect() or die $DBI::errstr;

Hope this helps,

--
     Jeff Zucker          Co-coordinator, UNICEF Voices of Youth
         \/                           http://www.unicef.org/voy/
        -<>-          
         /\                 CTO, Virtual Production Services LLC



Sun, 26 Aug 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Getting DBD::CSV to write the collumn names as first line of file

2. reading the first line off the file

3. First line skip when reading file

4. Read first line of a file

5. read in a file and delete first 8 lines

6. Extracting lines with duplicate first field

7. Finding first field in the last line in a database

8. How to parse a csv-file with optionally enclosed fields

9. Counting number of chars in a line (part of reading multiline CSV

10. dbl quoting fields of a CSV file

11. _value_ -1 in the first field of GLOBAL multi-field arrays

12. DBD::CSV can't find IO::File->new(name, mode)

 

 
Powered by phpBB® Forum Software