Mysql class works like php

Bruno Desthuilliers bruno.42.desthuilliers at
Fri Oct 5 09:28:09 CEST 2007

Andrey a écrit :
> Hi
> just a quick question about using MySQL module... are there any api / class 
> available to give a higher level in working with Mysql in python?
> such as
> db.fetch_array(),
> db.fetch_rows(),
> db.query(),
> for eachrow in db.fetch_array():
>     xxxx

You really find this "higher level" than Python's db-api ???

> just as easy as PHP?

D'oh :(

// PHP:
// suppose we have a valid $connection
$q = mysql_query("select * from yaddayadda", $connection)
if (is_resource($q)) {
   while($row = mysql_fetc_row($q)) {
else {
   // handle the error here

# python:
# suppose we have a valid connection
cursor = connection.cursor() # can specify the kind of cursor here
   cursor.execute("select * from yaddayadda")
except MysqlError, e:
   # handle error here
   for row in cursor:

# not strictly necessary, you can reuse the same
# cursor for another query

As far as I'm concerned, I fail to see how PHP is "higher level" or 
"easier" here.

More information about the Python-list mailing list