Getting DBD::CSV to write the collumn names as first line of file 
Author Message
 Getting DBD::CSV to write the collumn names as first line of file

This is starnge. I grabed some example code from a previous script  to
write a CSV file. I remember having a bit of fun getting DBD::CSV to _not_
write teh collumn names as the first line of that fiel.

Now I _want_ it to and it's not :-(

Here is what I am doing:

# All of this is for using the CSV module to write the output file
    $dbho = DBI->connect("DBI:CSV:f_dir=$save_dir;",
        ,  "csv_sep_char=,;csv_quote_char=;,csv_escape_char=;"
        ,  "csv_eol=")
        or die "Unable to connect: $DBI::errstr";

# Set CSV values the way Chris likes them
    $dbho->{'csv_tables'}->{$save_filename} = {
        'eol' => "\n",
        'sep_char' => "\;",
        'quote_char' => "\"",
        'escape_char' => "\"",
        'skip_first_row' => 0,

$stho = $dbho->prepare("$insert_string");

{

Quote:
}

But still no first row. What am I doing wrong here?


Tue, 24 Feb 2004 14:33:42 GMT  
 Getting DBD::CSV to write the collumn names as first line of file

Quote:

> This is starnge. I grabed some example code from a previous script  to
> write a CSV file. I remember having a bit of fun getting DBD::CSV to _not_
> write teh collumn names as the first line of that fiel.

> Now I _want_ it to and it's not :-(

> Here is what I am doing:

> # All of this is for using the CSV module to write the output file
>     $dbho = DBI->connect("DBI:CSV:f_dir=$save_dir;",
...
>     $dbho->{'csv_tables'}->{$save_filename} = {
...
> $stho = $dbho->prepare("$insert_string");
...

> {

> }
...

> But still no first row. What am I doing wrong here?

You haven't shown us any of the relevant code.  The code that is
relevant is the code used to create the file in the first place.  The
column names on the first line get put there when the file is created
(e.g. with a SQL CREATE statement), not when rows are inserted.  If your
"skip_first_row" wasn't set to 0 when the file was created, setting it
to 0 later will make no difference.

--
Jeff



Tue, 24 Feb 2004 16:41:48 GMT  
 Getting DBD::CSV to write the collumn names as first line of file

Quote:

>         'skip_first_row' => 0,


In addition to my previous comment about the relevant code being the
code used to create the file, you have told your script here *twice* not
to use the first row as column names:   1) if you want the first row to
be a list of column names, then skip_first_row should be 1 (i.e. it will
skip the first row when it looks for data since it knows that the first
row is a list of column names, not a data row) so by telling it to set
skip_first_row to 0 you are telling it there are no column names on the
first row and 2) by supplying a list of column names you are again
telling the script not to look on the first row for column names since,
as the docs explicitly state:

    you can supply an array ref of table names with the col_names
attribute.
    In that case the attribute skip_first_row will be set to FALSE.

So you've set skip_first_row to FALSE twice in your script and that is
exactly the opposite of what you say you want.

--
Jeff



Tue, 24 Feb 2004 16:54:53 GMT  
 Getting DBD::CSV to write the collumn names as first line of file

Quote:

>>         'skip_first_row' => 0,

>In addition to my previous comment about the relevant code being the
>code used to create the file, you have told your script here *twice* not
>to use the first row as column names:   1) if you want the first row to
>be a list of column names, then skip_first_row should be 1 (i.e. it will
>skip the first row when it looks for data since it knows that the first
>row is a list of column names, not a data row) so by telling it to set
>skip_first_row to 0 you are telling it there are no column names on the
>first row and 2) by supplying a list of column names you are again
>telling the script not to look on the first row for column names since,
>as the docs explicitly state:
>    you can supply an array ref of table names with the col_names
>attribute.
>    In that case the attribute skip_first_row will be set to FALSE.
>So you've set skip_first_row to FALSE twice in your script and that is
>exactly the opposite of what you say you want.

OK, I think you hit the problem with the first reply..
Thnaks.


Tue, 24 Feb 2004 17:34:52 GMT  
 Getting DBD::CSV to write the collumn names as first line of file
[snip]

Quote:
> $stho = $dbho->prepare("$insert_string");

> {

> }

> But still no first row. What am I doing wrong here?

Simple.  The first row of whatever you want has, for some reason, been

Show us the code which shows where $records_array_ref comes from, and
maybe we can tell you why it's missing the first row.

Oh, a couple of notes... you should use strict, use warnings, and take
advantage of the itertor that 'foreach' provides, and remember that a
bareword on the left side of the '=>' operator does not need to be
quoted.  Oh, and things will generally be simpler if you read the
docs... in particular, the stuff you have as parameters to connect() is
wrong.  It should be a single string, not a bunch of strings, and they
are split on semicolons, unless those are escaped... this means that [if
you fixed it so it was one string, not three], your connect line means:

$dbho = DBI->connect("DBI:CSV");
$dbho->{f_dir} = $save_dir;
$dbho->{csv_sep_char} = ",";
$dbho->{csv_quote_char} = "";
$dbho->{",csv_sep_char"} = "";
$dbho->{csv_eol} = "";

This is NOT what you want.  All you *really* want, is the connect line,
and maybe the f_dir part.  

Second... the keys of csv_tables are NOT filenames, they are
*tablenames* ... if you want to set the filename for a particular table,
then you should have it as the value of the 'file' subkey... that is.

$dbho->{csv_tables}{$tablename} = {
        file => $save_filename,
        ....

Quote:
};

Also... if the default value is good, you don't need to override it.
Since the default eol is "\n", and quote_char and escape_char are by
default "\"", you don't need to change them.  Also, setting col names
automatically sets skip_first_row, so you don't need to set that
explicitly.

#!/usr/bin/perl -w
use strict;
use DBI;
my $dbho = DBI->connect("DBI:CSV");
$dbho->{RaiseError} = 1;
$dbho->{csv_tables}{input_table_name} = {
        file => $i_filename,
        skip_first_row => 0,

Quote:
};


$dbho->{csv_tables}{output_table_name} = {
        file => $o_filename,
        sep_char => ";",

Quote:
}

my $input = $dbho->prepare(q[
        SELECT * from input_table_name
        WHERE ....
]);
my $output = $dbho->prepare(
        "INSERT INTO output_table_name (" .


);
$input->execute;
while( my $arow = $input->fetchrow_arrayref ) {

Quote:
}

--
"I think not," said Descartes, and promptly disappeared.


Fri, 27 Feb 2004 07:03:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

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

2. DBD::CSV with column names containing spaces

3. DBD::CSV or TEXT::CSV

4. How to read CSV files where the first line defines the field names?

5. DBD::CSV to read existing file

6. Which is broken DBD::Proxy or DBD::CSV

7. How to read/write Comma Seperated Value Files (CSV)

8. Perl to Write to /Read from CSV files

9. read files from dir then write as csv???

10. Help: file type name to write to a file

11. Net::FTP - getting only the first x bytes of a file

 

 
Powered by phpBB® Forum Software