Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using DBI:CSV to get data from a CSV file 1

Status
Not open for further replies.

nasar

IS-IT--Management
Aug 5, 2002
30
GB
I am trying to extract data from a CSV file using the code extract below, but it returns only four rows and they are all the same. If you point to what I am doing wrong or missing something that would be appreciated.

sample code:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my @row;

    my $dbh = DBI->connect("DBI:CSV:f_dir=.;csv_eol=\n;");
	
    my $sth = $dbh->prepare("SELECT * FROM test1.csv");
    $sth->execute();
	
    foreach (@row = $sth->fetchrow_array() ) {
	print "Row: @row\n";
    }

    $sth->finish();
    $dbh->disconnect();

The file test1.csv contains the following data:
Code:
"30360001","0","15","10"
"30360002","1","15","10"
"30360001","2","15","10"
"30360003","3","15","10"
"30360001","4","15","10"
"30360002","5","15","10"

I am running this on Windows XP platform.

Nasar
 
Thanks Steven,
That's worked a treat.

I have also amended the SQL to:
("SELECT * FROM test1.csv ORDER BY col1, col2");

I have to append a header row to my data file with column headings (I used "col1", "col2", "col3", "col4").

This allows me to sort my data file on two fields.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top