[Tutor] Exceptions while dealing with MySQL

Alan Gauld alan.gauld at btinternet.com
Sun Apr 22 01:00:14 CEST 2007


"Thanos Panousis" <pthanos at gmail.com> wrote

> I am developing a network management system that relies heavily on a
> MySQL database. The logic of the program is unavoidably coupled with
> query results I get from various tables.

That's pretty unusual, it normally indicates a non OO design.

> This is OK as long the mysql server has 100% uptime, but it hardly
> does.

Welcome to the real world... :-)

> thousand queries). What is the best approach to make error checking
> possible, with the least amount of code duplication?

Either write a generic wrapper around the execution of a query,
possibly by building a query class or just wrap all the queries in
try/except clauses....

> program to crash. It is really too much trouble checking for
> exceptions every time I make a simple query.

Welcome to the real world again.
In industrial strength code every file access, every database
query, every memory allocation (fortunately not often an issue
in Python!) and every network connection has to be protected.
Before try/except style programming that meant that around
75% of all code was error handling. With try/except we can
keep that down to 50%, sometimes less. But if reliability matters
you gotta catch the errors.

> What approach should I try take?

- genericize the queries so you catch  errors in one place.
- catch the errors
- do both if you have any doubts

or alternatively, pray....

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list