#!/usr/bin/perl use strict; use warnings; use DBI; sub RunSqlSearch; my $spreadsheet = 'newCsv.csv'; my $query = "SELECT sid,genotype, dnabl, dnatw12 " . "FROM $spreadsheet " . "WHERE genotype = 'a' " . "AND (IFNULL(dnatw12,0)/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_encoding => "utf-8", csv_eol => "\n", csv_null => 1, RaiseError => 1, }) or die $DBI::errstr; # Output using sql query # my $sth = $dbh->prepare($query); $sth->execute; my @row; my $queryResult=""; while (@row = $sth->fetchrow_array) { $queryResult .= join("\t",@row) . "\n"; } # output arguments # if ($queryResult eq ""){$queryResult = "No result found";} return ($queryResult); }