Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

Replies are listed 'Best First'.
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
    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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2016-09-25 21:19 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (473 votes). Check out past polls.