Hello paulski82,

I am not an SQL user, because I am working with MySQL so maybe my observations are wrong. But as far as I know SQL and MySQL are really close to each other so the syntax is the same 99% of the time.

A few things that I noticed on your code:

my $sql_query = "SELECT * FROM export"; my $dbh = DBI->connect ("dbi:CSV:f_dir:/root", undef, undef, { f_ext => ".csv", RaiseError => 1, } );

You are preparing a my $sql_query = "SELECT * FROM export"; before connecting.

Because you are executing the command after through:

my $sth = $dbh->prepare($sql_query); $sth->execute();

Your code will compile and execute without any problem, but it is a bit confusing in a human readable form and maybe also confuses you on the process.

I would suggest you modify your code like this:

$dbh = DBI->connect ("dbi:CSV:f_dir:/root", undef, undef, { f_ext => ".csv/r", RaiseError => 1, }) or die "Cannot connect: $DBI::errstr"; my $sql_query = "SELECT * FROM export"; my $sth = $dbh->prepare($sql_query); $sth->execute();

Observe the line that I have added or die "Cannot connect: $DBI::errstr", it will help you see a possible error if for any reason can not connect.

I would also suggest you modify the line:

my $sql_query = "SELECT * FROM export"; my $sth = $dbh->prepare($sql_query); $sth->execute();

To:

my $sth = $dbh->prepare ("SELECT * from export"); $sth->execute();

It is more clear what you are doing and also less coding lines make your code more efficient and faster.

At the end of your code when you finish, I would suggest to disconnect from your database.

Sample of code:

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

From the book Programming the Perl DBI written by Alligator Descartes & Tim Bunce, I have taken the following line:

The main activity in database programming usually involves the execution of SQL statements within a database. However, to accomplish this task, a connection to a database must be established first. Furthermore, after all the work has been done, it is good manners to disconnect from the database to free up both your local machine resources and, more importantly, valuable database resources.

It is important to free up both your local machine resources and, more importantly, valuable database resources.

As the rest of the monks suggested I think you are missing something small in the:

my $dbh = DBI->connect ("dbi:CSV:f_dir:/root", undef, undef, { f_ext => ".csv", RaiseError => 1, } );

I would suggest you to read the DBD::CSV - DBI driver for CSV files it contains code analysis and samples of code. I am sure by experimenting you will find your error.

Hope this helps.

Seeking for Perl wisdom...on the process...not there...yet!

In reply to Re: Trouble getting DBD::CSV to work by thanos1983
in thread Trouble getting DBD::CSV to work by paulski82

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.