Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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 ( [id://11112483]=note: print w/replies, xml ) Need Help??


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

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
  • Comment on Re: Perl DBI - Bind variable when query is inserted as part of db column
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found