Newbie needing some help

Chris Angelico rosuav at gmail.com
Fri Aug 8 20:13:24 EDT 2014


On Sat, Aug 9, 2014 at 9:55 AM, Chris Kaynor <ckaynor at zindagigames.com> wrote:
> try:
>     action
>     commit
> finally:
>     rollback

If commit/rollback automatically opens a new transaction, this would
just roll back an empty transaction - not a big deal. But yes, I do
see what you're looking at here.

However, structures like this are necessary only if you're hanging
onto the database connection. Python gives you a well-defined
unhandled-exception handler, and it's easy to just let exceptions
happen - if something goes wrong, you won't commit, and you'll get a
helpful traceback on the console. My recommended model for Python
databasing is:

Create database connection, get cursor
while "work to do":
    do work
Commit

Until such time as you have a demonstrable need for more complexity,
this model is safe, simple, and easy to work with. And less code
generally means less bugs :)

ChrisA



More information about the Python-list mailing list