Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: A Case with 5 Var's

by Corion (Pope)
on Jan 25, 2007 at 12:29 UTC ( #596459=note: print w/ replies, xml ) Need Help??


in reply to Re: A Case with 5 Var's
in thread A Case with 5 Var's

What kind of query? If you want to generate an SQL query from a hash, there is SQL::Abstract, which does just that:

use strict; use SQL::Abstract; my $sql = SQL::Abstract->new; my $table = 'customers'; my @fields = qw(name vorname plz tel); # You shouldn't have $name, $vorname etc. but a hash # containing the things you want: my %where = ( name => $name, vorname => $vorname, ); my ($stmt, @bind) = $sql->select($table, \@fields, \%where); print <<EOM; -- I use the following SQL: $stmt -- with the following placeholders @bind


Comment on Re^2: A Case with 5 Var's
Download Code
Re^3: A Case with 5 Var's
by ultibuzz (Monk) on Jan 25, 2007 at 12:36 UTC

    its an qry for a local unique system wich is not public, it needs defined stuff so i need to check if a var is filled or not so i can create a exact matching qry

    example: only $name and $plz is filled with Mueller and 55555 both are strings btw, then the qry looks like
    ::5544$nameGDFTxxxxx$plz::0:
    for each possibility is another qry defined ;/
    kd ultibuzz

      It depends how your query is built. Hopefully each variable is responsible for just one section, then you just build the query up as you go.

      my $qry = ""; my $name = "Mueller"; my $plz = "55555"; if ($name) { $qry .= "::5544$nameGDFT"; } if ($plz) { $qry .= "xxxxx$plz::0:";} print $qry;

      Since I have no idea what kind of query that is I had no idea what parts go where, but hopefully that gives you and idea how it can be done.

      If your query is completly dependent on the elements available then you could combine your ifs together some for readability at least

      if ($name && $vorname && $plz && $tel && tel49) { $qry = "whatever +" } elsif ($name && $vorname && $plz && $tel) { $qry = "whatever +2" } elsif ($name && $vorname && $plz) { $qry = "whatever +3" };

      That is not the recommended solution, just one of the many ways to do it.


      ___________
      Eric Hodges

        yep it depends on the elements, and with && i need much more if and elsifs.
        in my example i don't need to check $name later on when it was not there so its much shorter, all var's combination with && are a bit more ;)
        kd ultibuzz

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://596459]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-07-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (171 votes), past polls