Dear Monks, I am runnng an sql query using DBI. The code works well on my laptop running Strawberry Perl but fails to run on our university linux server using Perl 5.10.1
My perl progam is
#!/usr/bin/perl use strict; use warnings; use Text::CSV; use DBI; sub RunSqlSearch; my $query = qq(SELECT sid,genotype FROM newCsv.csv WHERE genotype = 'a +'); my ($queryResult,@sidResult) = RunSqlSearch($query); print "You query $query returned the following result:$queryResult\n"; print "The sid list associated with this result are: @sidResult\n\n"; sub RunSqlSearch($){ my $query = $_[0]; # error check # if ($query eq ""){die "No value entered for $query $!\n";} # Connect to the database, (the directory containing our csv file +(s)) my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => ".", f_ext => ".csv/r", f_encoding => "utf-8", RaiseError => 1, }) or die $DBI::errstr; # Output using sql query # my $sth = $dbh->prepare($query); $sth->execute; my @row; my $queryResult=""; my @queryResult; my @sidResult; while (@row = $sth->fetchrow_array) { push @queryResult, @row; push @sidResult, $row[0].","; $queryResult .= join("\t",@row) . "\n"; } # output arguments # if ($queryResult eq ""){$queryResult = "No result found";} return ($queryResult, @sidResult); $sth->finish(); $dbh->disconnect(); }
I get the following error message from our university server
DBD::CSV::st execute failed: Execution ERROR: No filename given at /usr/lib64/perl5/DBD/File.pm lin +e 565. called from runSQLQuery.pl at 11. at /usr/lib64/perl5/DBD/File.pm line 565. [for Statement "SELECT sid,genotype FROM newCsv.csv WHERE genotype = +'a'"] at runSQLQuery.pl line 32. DBD::CSV::st execute failed: Execution ERROR: No filename given at /usr/lib64/perl5/DBD/File.pm lin +e 565. called from runSQLQuery.pl at 11. at /usr/lib64/perl5/DBD/File.pm line 565. [for Statement "SELECT sid,genotype FROM newCsv.csv WHERE genotype = +'a'"] at runSQLQuery.pl line 32.
Could you please point out the error of my ways?
!!Fixed!!. Thank you, bretheren, for your wise counsel. The clue came from an anonymous brother who pointed out that there was no need for me to both specify the file extension name *.csv and also use the command f_ext => ".csv/r", When I did this I no longer received an error message, but I also got a nil return, which I new to be wrong.
Searching on stackoverflow, I found that in the act of cleaning up a and creating a csv file one can remove the DOS/Windows line ending which DBD::CSV assumes regardless of the platform the script is run on. Thanks to Grant McLean at http://stackoverflow.com/questions/2786741/dbdcsv-problem-with-file-name-extensions for this insight
The solution is to add the term csv_eol => "\n", as $dbh parameter.
In reply to DBD::CSV::st execute failed: by Sandy_Bio_Perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |