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


Groups > comp.lang.python > #75912 > unrolled thread

Re: Newbie needing some help

Started byChris Angelico <rosuav@gmail.com>
First post2014-08-09 08:36 +1000
Last post2014-08-09 08:36 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#75912 — Re: Newbie needing some help

FromChris Angelico <rosuav@gmail.com>
Date2014-08-09 08:36 +1000
SubjectRe: Newbie needing some help
Message-ID<mailman.12768.1407537382.18130.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web