this script pulls data from a cvs file, and creates a many to many relationship in an oracle db -
it hangs with this error msg. pls help before i pull out the rest of my hair!!
Name "main::c" used only once: possible typo at oraperldbi-st2.plx line 128.
Name "main::s" used only once: possible typo at oraperldbi-st2.plx line 129.
Use of uninitialized value at oraperldbi-st2.plx line 183, <CSV> chunk 1.
Use of uninitialized value at oraperldbi-st2.plx line 195, <CSV> chunk 1.
DBD::Oracle::st execute failed: ORA-00936: missing expression (DBD ERROR: OCIStmtExecute) at oraperldbi-st2.plx line 198, <CSV> chunk 1.
Couldn't execute statement: ORA-00936: missing expression (DBD ERROR: OCIStmtExecute) at oraperldbi-st2.plx line 198, <CSV> chunk 1.
\#!/usr/bin/perl -w
$ENV{'ORACLE_HOME'} = '/u00/app/oracle/product/8
.1.7';
$ENV{'ORACLE_SID'} = 'ORADEV';
use DBI;
use DBD::Oracle;
use Text::CSV;
my $file = 'tiexport.csv';
my $csv = Text::CSV->new();
$dbh = DBI->connect( 'dbi:Oracle:ORADEV',
'RISILL',
'RISILL92',
) || die "Database connection not made: $DBI::errstr";
open (CSV,$file) or die $!;
while (<CSV>) {
# next if ($. == 1);
if ($csv->parse($_)) {
my @columns = $csv->fields();
#print "@columns\n";
my $c3val=$columns[3];
my $lc3val=length($c3val);
if ( $lc3val < 4 ) {
$c3val="Empty";
}
my @d=split(/;;/,$c3val);
my $t=$columns[1];
foreach (@d)
{
$sbfld=$_;
$sl=length($sbfld);
my $codeloc=index($sbfld,'|n'
);
my $snameloc=index($sbfld,'|h
');
my $code=substr ($sbfld,0,$codeloc-1);
if ($c3val eq 'Empty')
{
$sname="no source";
}
else
{
$sname=substr ($sbfld,$codeloc +3,$snameloc-$codeloc);
$sname=~ s/\|h//g;
$sname=~ s/^\s+//;
$sname=~ s/\s+$//;
}
# IF NO PRICE DATA IN SUBFIELD
if ( index($sbfld,'|o') == -1 )
{
$hold=substr ($sbfld,$snameloc+2,$sl);
$hold=~ s/\|o//g;
}
# IF PRICE DATA
else
{
my $holdloc=index($sbfld,'|o'
);
$hold=substr ($sbfld,$snameloc+2,$holdl
oc);
$price=substr ($sbfld,$holdloc+2,$sl);
$hold=~ s/\|o//g;
}
#####SUB ADDSOURCE
sub addSource($sourcename,$sou
rcecode)
{
my ($sourcename, $sourcecode) = @_;
my $sthaddSource = $dbh->prepare("insert into source2
(sourceid,sourcename,sourc
ecode) values
(seq_source2_id.nextval,$s
ourcename,
$sourcecod
e)");
$sthaddSource->execute()
or die "Couldn't execute statement: " .
$sthaddSource->errstr;
$newsname= $dbh->quote( $sourcename ) ;
my $sth = $dbh->prepare("select
sourceid from
source2 where sourcename=$newsname");
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr;
if ( @sdata= $sth->fetchrow_array() )
{
$sourceid=$sdata[0];
}
return ($sourceid);
}
######SUB ADDTIT
sub addTit($newTit)
{
my ($newTit) = shift(@_);
$sthaddTit = $dbh->prepare("insert into title2
(title_id,title) values
(seq_titid2.nextval,$newTi
t)");
$sthaddTit->execute()
or die "Couldn't execute statement: " .
$sthaddTit->errstr;
$newTitle= $dbh->quote( $newTit ) ;
my $newsth = $dbh->prepare("select title_id from title2 where
title=$newTitle");
$newsth->execute()
or die "Couldn't execute statement: " . $newsth->errstr;
if ( @tdata= $newsth->fetchrow_array() )
{
$newtitleid=$tdata[0];
}
return ($newtitleid);
}
######TEST FOR SOURCE AND ADD IF NEEDED
my $sname2= $dbh->quote( $sname );
my $sth = $dbh->prepare("select
sourceid,sourcecode,source
name,sourc
eaddress,s
ourcephone
,sourcefax
from
source2 where sourcename=$sname2");
$sth->execute()
or die "Couldn't execute statement: " . $sth->errstr;
#IF SOURCE IS IN DB, GET SOURCEID, SOURCECODE, SOURCENAME
if ( @data= $sth->fetchrow_array() )
{
$i=$data[0];
$c=$data[1];
$s=$data[2];
}
#IF SOURCE NOT IN DB, CALL FUNCTION TO ADD IT
else
{
$newsid = &addSource($sname,$code);
print "Source $sname not found in db - callING ADDsource\n";
####$SEARCH=Y -- THE SOURCE JUST ADDED, USE $newsid
$search='y';
}
$sth->finish;
####END OF TEST FOR SOURCE
#####TEST FOR TITLE AND ADD IF NEEDED
my $t2= $dbh->quote( $t ) ;
my $sth2 = $dbh->prepare("select title_id,title from
title where title=$t2");
$sth2->execute()
or die "Couldn't execute statement: " . $sth2->errstr;
#####IF TITLE IS IN DB
if ( @data2 = $sth2->fetchrow_array() )
{
#####GET TITLEID AND TITLE
my $tid=$data2[0];
my $tit=$data2[1];
#####IF NEW SOURCE ID, USE NEWSID
if ($search eq 'y')
{
$sth3 = $dbh->prepare("insert into title_source
(title_sourceid,title_sour
ceno,sourc
e_titleno,
title_sour
cedata) values
(seq_tsid.nextval,$tid,$ne
wsid,$hold
)");
$sth3->execute()
or die "Couldn't execute statement: " . $sth3->errstr;
}
else
{
$sth3 = $dbh->prepare("insert into title_source
(title_sourceid,title_sour
ceno,sourc
e_titleno,
title_sour
cedata) values
(seq_tsid.nextval,$tid,$i,
$hold)");
$sth3->execute()
or die "Couldn't execute statement: " . $sth3->errstr;
}
}
#####IF TITLE NOT IN DB, CALL FUNC TO ADD IT
else
{
$newtid=&addTit($t2);
if ($search eq 'y')
{
$sth4 = $dbh->prepare("insert into title_source
(title_sourceid,title_sour
ceno,sourc
e_titleno,
title_sour
cedata) values
(seq_tsid.nextval,$newtid,
$newsid,$h
old)");
$sth4->execute()
or die "Couldn't execute statement: " . $sth4->errstr;
}
else
{
$sth4 = $dbh->prepare("insert into title_source
(title_sourceid,title_sour
ceno,sourc
e_titleno,
title_sour
cedata) values
(seq_tsid.nextval,$newtid,
$i,$hold)"
);
$sth4->execute()
or die "Couldn't execute statement: " . $sth4->errstr;
}
}
$sth2->finish;
$hold='';
$price='';
$i='';
$newsid='';
$search='';
}
} else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
close CSV;
$dbh->disconnect();
Start Free Trial