Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75912

Re: Newbie needing some help

References <CANhUTb9tEnT=uTMhdA+Fd2zJTMauo1BWkX2dgcE0dG_H5arx9Q@mail.gmail.com>
Date 2014-08-09 08:36 +1000
Subject Re: Newbie needing some help
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.12768.1407537382.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Aug 9, 2014 at 5:07 AM, Matt Smith <smithmm@tblc.org> wrote:
>         # Prepare SQL query to DELETE required records
>         sql = "DELETE FROM tblc_users WHERE user_email=%s, % (line)"
>         try:
>           # Execute the SQL command
>           cursor.execute(sql)
>           # Commit your changes in the database
>           db.commit()
>         except:
>           # Rollback in case there is any error
>           db.rollback()

By not even logging your errors, you mask everything that could go
wrong. There is an error in the block of code that I've highlighted
here, but the biggest problem is the bare except (as Mark pointed
out).

Suggestions:

1) Use a better subject line, which actually describes your problem.
2) As Mark said, don't use try/except here at all. Let exceptions
abort the whole process with a nice useful traceback.
3) Open a database connection once, right at the top, and don't commit
until the very end of your program.
4) If this isn't a toy program, consider doing the entire set of
deletions in a single statement - it'll allow the database to do a
single pass over the data. But if this is part of a "learn to use SQL"
course or something, ignore that, keep going the way you are.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Newbie needing some help Chris Angelico <rosuav@gmail.com> - 2014-08-09 08:36 +1000

csiph-web