Mysql class works like php

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Oct 5 03:28:09 EDT 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)) {
     do_something_with($row);
   }
   mysql_free($q);
}
else {
   // handle the error here
}

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

# not strictly necessary, you can reuse the same
# cursor for another query
cursor.close()


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