K�re Olai Lindbach [EMAIL PROTECTED] wrote:
> On Wed, 18 Jun 2003 06:51:39 -0400, you (Paul Boutros
> <[EMAIL PROTECTED]>) wrote:
> 
> >Quoting "Kovalcik, Mike A [ITS]" <[EMAIL PROTECTED]>:
> 
> >> I'm trying to setup some scripts that will allow me to use Perl DBI to
> >> INSERT into my table.  However, I have not had any success at all.  I'm
> >> using perl CGI as well so I've granted ALL permissions on my table to
> >> the apache user and I still can't INSERT.  I can, however, UPDATE and
> >> SELECT on the table, just not INSERT.  
> >> 
> >> Here is a piece of my code:
> >> 
> >> #--Establish the DB connection
> >> #--Assign the DB name
> >> $dbName = 'checkbook';
> >> 
> >> #--Connect to the Pg DB using DBI
> >> my $dbh = DBI->connect("dbi:Pg:dbname=$dbName");
> 
> Check for errors if this fails:
> 
> my $dbh = DBI->connect("dbi:Pg:dbname=$dbName") or die "No connection:
> $DBI::errstr";   # Or something else like croak/return etc instead of
> die...
> 
> >> $sth = $dbh->do("INSERT INTO transactions
> >> (date,description,amount,confirmation,nameid,typeid) VALUES
> >> ('$datePaid','$description','$amount','$confirmation',$nameid,$typeid)")
> >> ;
> 
> >Two things come to mind:
> >1. $dbh->do() doesn't return a statement handle, I think.  It just returns the 
> >count of rows affected.  What happens if you put a $dbh->commit after the do()?
> >2. It could also be a quoting issue.  Try using place-holders instead, with 
> >something like:
> >
> >my $sql = '
> >INSERT INTO transactions(
> >    date,
> >    description,
> >    amount,
> >    confirmation,
> >    nameid,
> >    typeid) 
> >VALUES (?,?,?,?,?,?)';
> >
> >my $sth = $dbh->prepare($sql);
> >
> >$sth->execute(
> >    $datePaid,
> >    $description,
> >    $amount,
> >    $confirmation,
> >    $nameid,
> >    $typeid);
> >
> >$dbh->commit();
> 
> ... and maybe use eval around the $sth->execute, and check for $@, or
> use $DBI::errstr

Read the perldocs on this by doing

   perldoc DBI

at a command prompt, and search for the section titled
"Transactions" - you'll see how to use AutoCommit, RaiseError
and 'eval' to trap DBI errors.

HTH.
-- 
Hardy Merrill
Red Hat, Inc.

Reply via email to