CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Zhris] Need help with perl script using DBI: Edit Log



terrykhatri531
User

Aug 20, 2014, 10:28 AM


Views: 8256
Re: [Zhris] Need help with perl script using DBI

Hi Chris,

I just want to check with you if the following script will work or not, I am not in a position to exexute it yet however I want to make sure it does work when I run it - Thank you !!


Code
  
#!/usr/local/bin/perl

use CGI;
use strict;
use DBI;
use warnings FATAL => qw(all);

my $q = new CGI;
my $pgh = pgh(); # connect to postgres
my $ora = ora(); # connect to oracle
my $sth;
my $dth;
my $placeholders;my @rows;

my @data=();
my @tables = qw!departments employees dept_emp dept_manager titles salaries!;
for my $t (@tables){
push @data,$q->param($t) || '' ;

push @data;

$sth = $pgh->prepare("SELECT * FROM @data");
$sth->execute();

while (my @rows = $sth->fetchrow_array()) {
$placeholders = join ", ", ("?") x @rows;
$dth = $ora->prepare("INSERT INTO @data VALUES($placeholders)");
$dth->execute(@rows);
}
}


#connect to postgres
sub pgh {
my $dsn = 'DBI:Pg:dbname=northwind;host=localhost';
my $user = 'postgres';
my $pwd = 'postgres';
my $pgh = DBI -> connect($dsn,$user,$pwd,{'RaiseError' => 1});
return $pgh;
}


#connect to oracle
sub ora {
my $dsn = 'dbi:Oracle:host=localhost;sid=orcl';
my $user = 'emp';
my $pwd = 'emp';
my $ora = DBI -> connect($dsn,$user,$pwd,{'RaiseError' => 1});
return $ora;
}


Many thanks !!

Terry


(This post was edited by terrykhatri531 on Aug 20, 2014, 10:42 AM)


Edit Log:
Post edited by terrykhatri531 (User) on Aug 20, 2014, 10:30 AM
Post edited by terrykhatri531 (User) on Aug 20, 2014, 10:38 AM
Post edited by terrykhatri531 (User) on Aug 20, 2014, 10:41 AM
Post edited by terrykhatri531 (User) on Aug 20, 2014, 10:42 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives