http://www.perlmonks.org?node_id=1004473

kulls has asked for the wisdom of the Perl Monks concerning the following question:

Hi
, Is there any CPAN Module which helps me to construct the DB query based on given parameter as an argument ?
-Raja
  • Comment on Dynamic Query construct based on argument

Replies are listed 'Best First'.
Re: Dynamic Query construct based on argument
by ColonelPanic (Friar) on Nov 19, 2012 at 08:50 UTC

    Well, DBI, Perl's standard way of interfacing with databases, lets you do just that. You can make an SQL statement with parameters that can be set later:

    my $dbh = DBI->connect(...connection parameters...); my $get_foo = $dbh->prepare("select foo from table where bar=?"); $get_foo->execute(3); #select foo where bar=3 $get_foo->execute($baz); #select foo where bar=$baz

    Perhaps you meant something different by constructing a query. If so, you need to specify what you mean. SQL is a whole programming language in itself.



    When's the last time you used duct tape on a duct? --Larry Wall
Re: Dynamic Query construct based on argument
by CountZero (Bishop) on Nov 19, 2012 at 11:21 UTC
    Perhaps SQL::Abstract can help you. It transforms a Perl data-structure into valid SQL.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics