#!/usr/bin/perl use strict; use warnings; use Text::CSV; use DBI; sub RunSqlSearch; my $spreadsheet = 'newCsv.csv'; my $query = qq(SELECT sid,genotype, dnabl, dnatw12 FROM $spreadsheet WHERE genotype = 'a' AND (dnatw12/dnabl)<0.5 AND dnatw12 IS NOT NULL ); my ($queryResult) = RunSqlSearch($query); print "Your query $query returned the following result:\n$queryResult\n"; sub RunSqlSearch($){ my $query = $_[0]; # error check # if ($query eq ""){die "From runSqlQuery2 - 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", csv_eol => "\n", RaiseError => 1, }) or die $DBI::errstr; # Output using sql query # my $sth = $dbh->prepare($query); if($sth){ $sth ->finish(); } $sth->execute; my @row; my $queryResult=""; my @queryResult; while (@row = $sth->fetchrow_array) { push @queryResult, @row; $queryResult .= join("\t",@row) . "\n"; } # output arguments # if ($queryResult eq ""){$queryResult = "No result found";} return ($queryResult); $sth->finish(); $dbh->disconnect(); }