Mysql class works like php

A

Andrey

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
 
B

Bruno Desthuilliers

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.
 
G

gardsted

Bruno said:
Andrey a écrit :

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


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
 
B

Bruno Desthuilliers

gardsted a écrit :
(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
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top