Mysql in Python?

Ludovico Magnocavallo ludo at asiatica.org
Sat Aug 28 20:01:35 EDT 2004


Simon John wrote:
> I got MySQLdb working with MySQL4, it's pretty similar to the PHP
> implementation, although error handling isn't great (nor is
> documentation!)

What are you finding "not great" in MySQLdb's error handling? It 
implements the exceptions defined in the DB-API, which I find way better 
than PHP's mysql_errno() and friends:

try:
     cursor.execute(some_statement)
except MySQLdb.IntegrityError, e:
     # handle a specific error condition
except MySQLdb.Error, e:
     # handle a generic error condition
except MySQLdb.Warning, e:
     # handle warnings, if the cursor you're using raises them

What can be simpler than that? Or do you prefer PHP's way, which gives 
you no direct, portable way of knowing what kind of error was returned 
by the DB:

$result = mysql_query($statement, $connection);
if (!$result) {
     $error = mysql_error();
     $errno = mysql_errno();
     // $error and $errno are mysql-specific
}

Ludo



More information about the Python-list mailing list