in reply to seeking barebones sqlite example with transaction

I don't know about SQLite specifically, but not every DBD driver supports compound statements like that.

The portable way to do transactions with DBI is by using begin_work, and commit/rollback.

Usually you'd wrap the transaction in an eval to catch errors, so the following would be the more common idiom (copied from the Transactions section in the DBI docs):

$dbh->begin_work; eval { local $dbh->{RaiseError} = 1; foo(...) # do lots of work here bar(...) # including inserts baz(...) # and updates $dbh->commit; # commit the changes if we get this far }; if ($@) { warn "Transaction aborted because $@"; # now rollback to undo the incomplete changes # but do it in an eval{} as it may also fail eval { $dbh->rollback }; # add other application on-error-clean-up code here }

Update: Added RaiseError code per runrig's comment.

Replies are listed 'Best First'.
Re^2: seeking barebones sqlite example with transaction
by runrig (Abbot) on Aug 04, 2006 at 22:19 UTC
    Note that for catching errors with eval like that, you should turn on the RaiseError attribute (upon connect or afterward). (And thanks, I didn't notice the begin_work() method to temporarily turn off AutoCommit before now).
      Heh, I was about to update my node to that effect. The Transaction section in the DBI docs does mention turning on RaiseError. Strangely enough, it doesn't use the begin_work method, although I think it's a cleaner way of writing this pattern.

      I looked at the code of begin_work, and it doesn't touch RaiseError. I suppose that makes sense in general, but I'd expect such a handy method to do that for me.