Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^2: DBD::CSV::prepare() problems

by polelo (Initiate)
on Aug 24, 2012 at 20:02 UTC ( #989631=note: print w/replies, xml ) Need Help??

in reply to Re: DBD::CSV::prepare() problems
in thread DBD::CSV::prepare() problems

Dear Tux

Thanks for your reply

this is how I connect to the CSV database

use DBI; ... #MS Windows path to files my $fpath="G:\\in"; #LINUX server paths #my $fpath="/users/excel/in"; my $csvfile="Trx_24Aug2012_0409.csv"; my $cellh = DBI->connect("DBI:CSV:f_dir=$fpath;csv_eol=\n;"); #associate csv file with tablename my $fname=$fpath.'/'.$csvfile; $cellh->{'csv_tables'}->{'CELLS'} = {'file' =>$fname}; my $sql="select * from CELLS"; my $stcellh = $cellh->prepare($sql) or die "$DBI::errstr\n"; $stcellh->execute() or die "$DBI::errstr\n"; my $colunmcount = $stcellh->{NUM_OF_FIELDS}; print "$colunmcount\n\n"; $stcellh->finish();

Thanks again


Replies are listed 'Best First'.
Re^3: DBD::CSV::prepare() problems
by Tux (Abbot) on Aug 25, 2012 at 21:20 UTC

    Because you use the file => $filename approach, you do not use the case-insensitiveness from SQL::Statement.

    I do not think you should define csv_eol

    When on Windows *and* other OS's, do not use $dir."/".$file, but use File::Spec.

    When in doubt about casing, use something like

    chdir $f_path; foreach my $file_name (glob "*.*") { lc $file_name eq lc $csvfile or next; $csv_file = $file_name; last; } my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => $f_path, f_ext => ".csv/r", f_encoding => "utf-8", f_schema => undef, RaiseError => 1, PrintError => 1, }); $cellh->{csv_tables}{cells} = { file => $csv_file }; my $sth = $sbh->prepare ("select * from cells"); $sth->execute; print "fields: @{[@{$sth->{NAME_lc}}]}\n";

    untested, but should work.

    Enjoy, Have FUN! H.Merijn

      When on Windows *and* other OS's, do not use $dir."/".$file, but use File::Spec.

      Go ahead and use / on every OS you have

        Go ahead and use / on every OS you have

        Nonsense! Not every OS uses the forward slash (/) as path separator.

        DOS and Windows (and OS/2, I think) allow to use both backslash (\) and forward slash, so you can use the forward slash on Windows, except when you try to use external tools that expect that you only use the backslash.

        On a classic, non-X MacOS, you will fail miserably because MacOS uses single colons (:).

        Same game, different rules with VMS: / works only in Unix emulation, the native interface uses single colons, double colons (C<::>), square brackets ([]), angle brackets (<>), periods (C<.>), and semicolons (C<;>) to separate the parts of a fully qualified file name.

        File::Spec is not perfect, but it still is better than simply using the forward slash everywhere.


        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^3: DBD::CSV::prepare() problems
by remiah (Hermit) on Aug 25, 2012 at 10:32 UTC
    Hello Polelo

    I tried with my FreeBSD but it says nothing for filename. No error message, no warnings.

    #!/usr/bin/perl use strict; use warnings; use DBI; my $dsn="dbi:CSV:"; my $dbh=DBI->connect($dsn, "","",{ f_encoding => "utf8", f_dir => "./", })or die DBI->errstr; $dbh->{csv_tables}->{t1} = { 'file' => '17_Trx_22Aug2012_0405.csv', 'eol' => "\n", }; my $sth; $sth=$dbh->prepare("select * from t1"); $sth->execute; while( my $r = $sth->fetchrow_hashref){ print "$r->{item1},$r->{item2}\n"; } $sth->finish; $dbh->disconnect; #print version print "perl version =$]\n"; for (qw/DBI Text::CSV_XS SQL::Statement DBD::CSV/){ print "$_ ==>".$_->VERSION."\n"; }
    versions of mine. How is yours?
    perl version =5.012002
    DBI ==>1.615
    Text::CSV_XS ==>0.85
    SQL::Statement ==>1.33
    DBD::CSV ==>0.35

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2017-10-18 04:38 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (242 votes). Check out past polls.