If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > Newbie question - update in loop

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-03-06, 13:08
ssmith001 ssmith001 is offline
Registered User
 
Join Date: Sep 2005
Posts: 220
Newbie question - update in loop

I am very new to Perl and I need some help. I am trying to write some code that will accept one or more location numbers from the command line, and then will update a row in an Oracle database. I have code that will connect to the DB, but I'm not sure how to loop through and do the update until I run out of parameters on the command line. Can someone give me a hand?

Code:
#!/usr/bin/perl -w

use DBI;
use strict;

my $instance = 'mdip1';
my $data_source = "dbi:Oracle:$instance";
my $user = "abc";
my $password ="abc";
my $dbh = DBI->connect($data_source, $user, $password, {
RaiseError => 1});

my $sth = $dbh->prepare( q{ UPDATE stsc.sku
		SET p_abc = 'D', 
    		p_abcunitstotqty = 0
		WHERE loc = ? 
		AND p_manualparameters = 0
		AND p_abc in ('A','B','C') });
						 
$dbh->disconnect;
Reply With Quote
  #2 (permalink)  
Old 02-04-06, 06:31
reneeb reneeb is offline
Registered User
 
Join Date: Jan 2004
Location: Germany
Posts: 167
if you want to use the locs as parameters:
Code:
#!/usr/bin/perl -w

use DBI;
use strict;

my $instance = 'mdip1';
my $data_source = "dbi:Oracle:$instance";
my $user = "abc";
my $password ="abc";
my $dbh = DBI->connect($data_source, $user, $password, {
RaiseError => 1}) or die $DBI::errstr;

my $sth = $dbh->prepare( q{ UPDATE stsc.sku
		SET p_abc = 'D', 
    		p_abcunitstotqty = 0
		WHERE loc = ? 
		AND p_manualparameters = 0
		AND p_abc in ('A','B','C') }) or die $dbh->errstr();

for my $loc(@ARGV){
  $sth->execute($loc) or die $dbh->errstr();
}
						 
$dbh->disconnect;
Usage: skript.pl <loc1> <loc2> <loc3> ...


And if you want to read from a file:
Code:
#!/usr/bin/perl -w

use DBI;
use strict;

my $instance = 'mdip1';
my $data_source = "dbi:Oracle:$instance";
my $user = "abc";
my $password ="abc";
my $dbh = DBI->connect($data_source, $user, $password, {
RaiseError => 1}) or die $DBI::errstr;

my $sth = $dbh->prepare( q{ UPDATE stsc.sku
		SET p_abc = 'D', 
    		p_abcunitstotqty = 0
		WHERE loc = ? 
		AND p_manualparameters = 0
		AND p_abc in ('A','B','C') }) or die $dbh->errstr();

open(my $fh,"<",$ARGV[0]) or die $!;
while(my $line = <$fh>){
  chomp $line;
  $sth->execute($line) or die $dbh->errstr();
}
close $fh;
						 
$dbh->disconnect;
usage: skript.pl <filename>
__________________
board.perl-community.de - The German Perl-Community
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On