NodeReaper has asked for the wisdom of the Perl Monks concerning the following question:

  • Comment on (Duplicate) DBI conditional insert (and things of those nature) (To Be Deleted...)

Replies are listed 'Best First'.
Re: DBI conditional insert (and things of those nature)
by trs80 (Priest) on Jan 25, 2002 at 23:53 UTC
    You can improve this by using placeholders.
    use DBI; $DSN = "dbi:Oracle:host=localhost;sid=essinv;port=1521"; $user = "username"; $pw = "password"; $dbh = DBI->connect($DSN,$user,$pw, { RaiseError => 1, AutoCommit +=> 1}) || die "Cannot connect: $DBI::errstr\n" unless $dbh; print header (); eval{ $SQL = "INSERT INTO EMPLOYEE ( EMPLOYEE_NUMBER, FIRST_NAME, LAST_NAME, INFORMATION_D +ATE) VALUES ( ? , ? , ? , SYSDATE)"; $sth = $dbh->prepare($SQL); }; # End of eval # Check for errors. if($@){ $dbh->disconnect; print "Content-type: text/html\n\n"; print "An ERROR occurred! $@\n<P>"; exit; } else { $sth->execute(param('employee_number'),param('first_name'),param( +'last_name')); } # End of if..else sub print_output { print <<HTML; PRINT CONFIRMATION HERE HTML } # Disconnect from the database $sth->finish; $dbh->disconnect;
    As for finding if it exists first this may or may not be needed depending on the database structure. How the item is created/maintained will alter my idea of what the best solution would be.