Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Perl DBI - Bind variable when query is inserted as part of db column

by saurabh220837 (Initiate)
on Feb 04, 2020 at 17:56 UTC ( [id://11112379]=perlquestion: print w/replies, xml ) Need Help??

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

I have a query(query1), which has some bind variables in the query. This query needs to be inserted into another query(query2), and all bind variables should be replaced in query1
query1 - select * from table1 where column1=? and column2=? push @bindparams, $colum1 push @bindparams, $colum2 query2 - insert into table2 values ('a1',$query1)
issue is , query1 should be bound with @bindparams as well, when inserted as part of query2
  • Comment on Perl DBI - Bind variable when query is inserted as part of db column
  • Download Code

Replies are listed 'Best First'.
Re: Perl DBI - Bind variable when query is inserted as part of db column
by choroba (Cardinal) on Feb 04, 2020 at 21:36 UTC
    What do you mean by "inserting a query into another query"?

    1. Maybe you want to insert the results of the first query into the second one. This example shows how to do it with DBD::SQLite, some other databases can't have more than one prepared statement per db handle, so you'd need to connect more than once.

    2. Or you want to create a "SELECT INTO" statement. SQLite doesn't support this syntax directly, but features a similar construct that you can use:

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Perl DBI - Bind variable when query is inserted as part of db column
by Marshall (Canon) on Feb 06, 2020 at 09:46 UTC
    I don't think this is a case to use explicit bind vars.

    Assuming that you want to copy some columns from table1 to a new table2 based upon some parameters in table1 that won't even appear in table2, here is some code that was adapted from choroba's post:

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use DBI; my $db = 'DBI'->connect('dbi:SQLite:;db=:memory:', "", ""); $db->do('CREATE TABLE t1 (id INT, name TEXT, quantity INT)'); $db->do('CREATE TABLE t2 (id INT, quantity INT)'); my $populate = $db->prepare( 'INSERT INTO t1 (id, name, quantity) VALUES (?, ?, ?)'); $populate->execute(@$_) #some test cases that don't start with 'J' for [ 1, 'John', 12 ], [ 2, 'Jane', 14 ], [ 3, 'Jack', 16 ], [ 5, 'Joe', 22 ], [ 5, 'bobo', 33 ], [ 23,'xyz', 44 ], [ 6, 'Josh', 27 ]; my $copy = $db->prepare("INSERT INTO t2 (id, quantity) SELECT id, quantity FROM t1 WHERE name LIKE ? AND id > ?"); $copy->execute('J%',2); my $verify_t2 = $db->prepare('SELECT * FROM t2'); $verify_t2->execute; my @row; say "@row" while @row = $verify_t2->fetchrow_array; __END__ Prints: 3 16 5 22 6 27
Re: Perl DBI - Bind variable when query is inserted as part of db column
by Anonymous Monk on Feb 06, 2020 at 14:28 UTC

    "In the simplest case, bind parameters are resolved left-to-right." If the query involves a subquery, it's still the same: "left to right."

    Perhaps for this reason, though, it's also possible to find modules on CPAN that let you substitute the parameters symbolically: "where column1={col1} and..." They use the symbols to correctly build the parameter-list in proper order and then replace the symbols {col1} by the "?" that is required by SQL.

      possible to find modules on CPAN

      So, you actually know of at least one such module? And you're too much of an expert programmer to link it, or even name it? Wow. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11112379]
Approved by Corion
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-24 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found