Newbie needing some help

Chris Angelico rosuav at gmail.com
Fri Aug 8 18:36:13 EDT 2014


On Sat, Aug 9, 2014 at 5:07 AM, Matt Smith <smithmm at 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



More information about the Python-list mailing list