Home » Coding » Python » Mysql class works like php
Mysql class works like php [message #15139] Thu, 04 October 2007 19:09 Go to next message
Andrey  is currently offline Andrey
Messages: 2
Registered: September 2007
Junior Member
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

just as easy as PHP?
I think someone might already done this, so i dont have to re-invent the
wheel, but i have no luck in google, so wondering if anyone might know such
thing exists...

thanks
Andrey
Re: Mysql class works like php [message #15218 is a reply to message #15139 ] Fri, 05 October 2007 03:28 Go to previous messageGo to next message
Bruno Desthuilliers  is currently offline Bruno Desthuilliers
Messages: 277
Registered: July 2007
Senior Member
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.
Re: Mysql class works like php [message #15620 is a reply to message #15218 ] Sun, 07 October 2007 04:48 Go to previous messageGo to next message
gardsted  is currently offline gardsted
Messages: 1
Registered: October 2007
Junior Member
Bruno Desthuilliers wrote:
> 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.
>
>

Maybe You should look into sqlalchemy.
I am also a newbie at that, but it allows you to do things like this
(untested):
sqltxt="""select * from mytable"""
result=mysession.execute(sqltxt)
for r in result.fetchmany():

which pretty good mimics the 'for eachrow in db.fetch_array():'

yours politely
jorgen
Re: Mysql class works like php [message #15665 is a reply to message #15620 ] Sun, 07 October 2007 08:40 Go to previous messageGo to next message
Bruno Desthuilliers  is currently offline Bruno Desthuilliers
Messages: 277
Registered: July 2007
Senior Member
gardsted a écrit :
> Bruno Desthuilliers wrote:
>> 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
>>
(snip)
> Maybe You should look into sqlalchemy.
> I am also a newbie at that, but it allows you to do things like this
> (untested):
> sqltxt="""select * from mytable"""
> result=mysession.execute(sqltxt)
> for r in result.fetchmany():
>
> which pretty good mimics the 'for eachrow in db.fetch_array():'

SQLAlchemy is quite a good package (and *way* higher level than anything
in PHP), but you don't need it to do the above. Any db-api compliant
package will do:

sql = "SELECT * FROM yaddayadda"
cursor.execute(sql)
for row in cursor.fetchall():
# code here
Re: Mysql class works like php [message #17425 is a reply to message #15665 ] Sun, 07 October 2007 15:16 Go to previous message
Dennis Lee Bieber  is currently offline Dennis Lee Bieber
Messages: 55
Registered: August 2007
Member
On Sun, 07 Oct 2007 14:40:38 +0200, Bruno Desthuilliers
<bruno.42.desthuilliers@wtf.websiteburo.oops.com> declaimed the
following in comp.lang.python:

> SQLAlchemy is quite a good package (and *way* higher level than anything
> in PHP), but you don't need it to do the above. Any db-api compliant
> package will do:
>
> sql = "SELECT * FROM yaddayadda"
> cursor.execute(sql)
> for row in cursor.fetchall():
> # code here

And the good ones don't even need the .fetchall() to iterate...

for row in cursor:
...

However, if one just wants a raw list of tuples, then...

rlot = cursor.fetchall()

--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/
Previous Topic:Putting a line from a text file into a variable, then moving to nextline
Next Topic:Override 'and' and 'or'
Goto Forum:
  


Current Time: Fri May 16 02:39:16 EDT 2008

Total time taken to generate the page: 0.24390 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software