Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: CSV column names

by Mr. Muskrat (Canon)
on Aug 23, 2016 at 17:35 UTC ( #1170243=note: print w/replies, xml ) Need Help??


in reply to CSV column names

If you haven't enabled skip_first_row (you have not in this script) and info.csv has all of the column names defined in the first row then you should be able to use col_names.

I just threw this together. It is untested!

#!/bin/env perl use strict; use warnings; use DBI; my $dbh = DBI->connect('DBI:CSV:'); $dbh->{csv_tables}{info} = { file => 'info.csv' }; my $qu = $dbh->prepare('select * from info'); $qu->execute(); my @cols = @{$dbh->{csv_tables}{info}{col_names}}; # col_names won't b +e defined until you've queried the file. print "column names: @cols\n"; while( my @row = $qu->fetchrow_array) { print "row: @row\n"; }

Update: Corrected script.

Replies are listed 'Best First'.
Re^2: CSV column names
by Marshall (Abbot) on Aug 23, 2016 at 21:45 UTC
    I tried this with one of my CSV files.

    Not sure what is going on.

    #!/bin/env perl use strict; use warnings; use DBI; my $dbh = DBI->connect('DBI:CSV:'); $dbh->{csv_tables}{info} = { file => '2011-SSCW.csv' }; #Gives 'Not an ARRAY reference at C:\Projects_Perl\SSPH_Results\Muskra +t.pl line 11.' #my @cols = @{$dbh->{csv_tables}}; #print "column names: @cols\n"; my $qu = $dbh->prepare('select * from info'); $qu->execute(); ## for unknown reasons, fetchrow_array returns undef values ## I hacked this in to get the CSV contents while( my @row = grep{defined $_}$qu->fetchrow_array) { print "row: @row\n"; #I get the CSV file ok, here }

      I've updated my script now that I have had a chance to try it.

Re^2: CSV column names
by Anonymous Monk on Aug 27, 2016 at 10:49 UTC

    Sorry to bother you once more. When i use sql to make the table(csv.file), once again the column names are not appearing. So the script is not working. Any ideas?

      "working", "not working" ? -- please be more explicit: what happens when you do what, exactly? What and why is unexpected?

      Post the code and the first few lines of the files (10 lines or so will do).

        When i run the following script

        #!/bin/env perl use strict; use warnings; use DBI; my $dbh = DBI->connect('DBI:CSV:'); $dbh->{csv_tables}{info} = { file => 'info.csv' }; my $qu = $dbh->prepare('select * from info'); $qu->execute(); my @cols = @{$dbh->{csv_tables}{info}{col_names}}; # col_names won't b +e defined until you've queried the file. print "column names: @cols\n"; while( my @row = $qu->fetchrow_array) { print "row: @row\n"; }
        It won't work if the table(info) was made using sql students

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1170243]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2017-12-17 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (459 votes). Check out past polls.

    Notices?