todd has asked for the wisdom of the Perl Monks concerning the following question:

<html> <head></head> <body>
Hello Monks,

I'm having a problem using DBI:CSV. 

Assuming the existence of a file ./test_db which looks like

f1,f2,f3,f4,f5

Can someone explain to me why the following code works

#!/usr/bin/perl 

use DBI;

my $dbh = DBI->connect("DBI:CSV:")
    or die "Can't connect: " . $DBI::errstr;
my @fields = qw(one two three four five);

$dbh->do("INSERT INTO test_db VALUES(?,?,?,?,?)",undef,one, two, three, four, five);

$dbh->disconnect;

## End code

Yet the following doesn't

#!/usr/bin/perl 

use DBI;

my $dbh = DBI->connect("DBI:CSV:")
    or die "Can't connect: " . $DBI::errstr;
my @fields = qw(one two three four five);

my $sql=qq`"INSERT INTO test_db VALUES (?,?,?,?,?)"`  . ",undef," . join( ',' ,@fields);


$dbh->do($sql);

$dbh->disconnect;

## End code

This fails with the error

DBD::CSV::db do failed: Parse error near "INSERT INTO test_db VALUES (?,?,?,?,?)",undef,one,two,three,four,five at /usr/lib/perl5/site_perl/5.005/DBD/File.pm line 164.

Thank you in advance.

Todd
</body> </html>

Replies are listed 'Best First'.
RE: DBI:CSV problem
by chromatic (Archbishop) on Sep 20, 2000 at 22:37 UTC
    You also have an internal set of quotes on the second SQL statement. Here's a better solution:
    $dbh->do(q{ INSERT INTO test_db VALUES (?,?,?,?,?) } undef, @fields);
    That's lifted straight out of the Perl DBI book. Not only do you need to pass values to fill in placeholders as list elements, you don't need an extra set of quotes when you use the q// operator. They're implied.

    Here's what I think you might have been trying to do:

    my $sql = q{INSERT INTO test_db VALUES(} . join(', ', (undef, @fields)). ')';

RE: DBI:CSV problem
by little (Curate) on Sep 20, 2000 at 15:56 UTC
    Hi todd,

    while in your first code example you are passing an parameter list, in your second example you are passing a string.
    So I've tried

    eval('$dbh->do('.$sql.');');

    which ran without errors. But I guess there's a more nice way in doing this also. So far ...
Re: DBI:CSV problem
by Anonymous Monk on Sep 20, 2000 at 16:37 UTC
    <html> <body>
    Hi little,
    
    Thanks very much for your quick response.
    It works great.
    
    cheers 
    
    todd
    
    </body> </html>