Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Perl -connecting to DB taking query input from user

by MVRS (Acolyte)
on Mar 06, 2013 at 01:28 UTC ( #1021923=perlquestion: print w/replies, xml ) Need Help??
MVRS has asked for the wisdom of the Perl Monks concerning the following question:

my code unable take the input from the User input $Strain my out put is nareshmvr@ubuntu:~/Desktop/db/sampledb$ perl usr_connectdb.pl Please Enter the Strain Name : M18 ssnareshmvr@ubuntu:~/Desktop/db/sampledb$

#!/usr/bin/perl -w #use Strict; use DBI; print "Please Enter the Strain Name : \n"; my $Strain = <>; $dbh = DBI->connect('dbi:mysql:sampledb','root','******') or die "Connection Error: $DBI::errstr\n"; $sql = "SELECT feature.fastaId,feature.contigId,feature.orfId,strain. +strainName,sequence.ntseq,sequence.aaseq from feature left join strai +n on feature.id=strain.id left join sequence on feature.id=sequence.i +d where strainName ='$Strain';"; print "ss"; $sth = $dbh->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; while (@row = $sth->fetchrow_array) { print "@row\n"; }

Replies are listed 'Best First'.
Re: Perl -connecting to DB taking query input from user
by graff (Chancellor) on Mar 06, 2013 at 02:22 UTC
    If you really want to prompt for input from the user after the program starts, then the advice in the first reply will take care of the immediate problem. But I would recommend using a command-line argument, which your script sees as $ARGV[0] (you run your script via a command-line shell, don't you?)

    Apart from that, you'll be better off using a placeholder in the sql query string, and passing the value to search for as a parameter when you execute the query:

    #!/usr/bin/perl use strict; # don't comment this out, and don't capitalize it. use DBI; my $Strain; if ( @ARGV ) { $Strain = shift; # get your target string from the command line } else { print "Please Enter the Strain Name: "; $Strain = <>; chomp $Strain; } $dbh = DBI->connect('dbi:mysql:sampledb','root','******') or die "Connection Error: $DBI::errstr\n"; $sql = <<EOS; # let's make it easier to read while we're at it SELECT feature.fastaId, feature.contigId, feature.orfId, strain.strainName, sequence.ntseq, sequence.aaseq from feature left join strain on feature.id=strain.id left join sequence on feature.id=sequence.id where strainName = ? EOS $sth = $dbh->prepare($sql); $sth->execute( $Strain ) or die "SQL Error: $DBI::errstr\n"; while (@row = $sth->fetchrow_array) { print "@row\n"; }

      thank you graff its working fine

Re: Perl -connecting to DB taking query input from user
by kielstirling (Scribe) on Mar 06, 2013 at 01:51 UTC
    I don't understand?? But have noticed that you are passing $Strain with a \n on it. Try
    chomp(my $Strain = <>);
    see perldoc -f chomp
      Insted of giving Where strainName = 'xxxxx'in the query , i want to give it as the user input

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1021923]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-07-20 01:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (422 votes). Check out past polls.

    Notices?