Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.
    #!/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(@$_) for [ 1, 'John', 12 ], [ 2, 'Jane', 14 ], [ 3, 'Jack', 16 ], [ 5, 'Joe', 22 ], [ 6, 'Josh', 27 ]; my $insert = $db->prepare('INSERT INTO t2 (id, quantity) VALUES (?, ?) +'); my $query1 = $db->prepare( 'SELECT id, quantity FROM t1 WHERE name LIKE ? AND id > ?'); $query1->bind_param(1, 'J%'); $query1->bind_param(2, 2); $query1->execute; while (my @row = $query1->fetchrow_array) { $insert->execute(@row); } my $verify = $db->prepare('SELECT * FROM t2'); $verify->execute; my @row; say "@row" while @row = $verify->fetchrow_array;

  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:
    #!/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, action INT)'); my $populate = $db->prepare( 'INSERT INTO t1 (id, name, quantity) VALUES (?, ?, ?)'); $populate->execute(@$_) for [ 1, 'John', 12 ], [ 2, 'Jane', 14 ], [ 3, 'Jack', 16 ], [ 5, 'Joe', 22 ], [ 6, 'Josh', 27 ]; my $insert = $db->prepare( 'INSERT INTO t2 (action, id, quantity)' . ' SELECT ?, id, quantity FROM t1 WHERE name LIKE ? AND id > ?'); $insert->bind_param(1, 100); $insert->bind_param(2, 'J%'); $insert->bind_param(3, 2); $insert->execute; my $verify = $db->prepare('SELECT * FROM t2'); $verify->execute; my @row; say "@row" while @row = $verify->fetchrow_array;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

    In reply to Re: Perl DBI - Bind variable when query is inserted as part of db column by choroba
    in thread Perl DBI - Bind variable when query is inserted as part of db column by saurabh220837

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 05:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found