473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Mysql class works like php

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

Oct 4 '07 #1
3 1685
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.
Oct 5 '07 #2
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
Oct 7 '07 #3
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

Oct 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mdh | last post by:
I am trying to learn the basics of MVC applications using a Tomcat infrastructure. I'm starting by building a simple application with: * a login.jsp page for a basic login form with a action...
0
by: Robert Mazur | last post by:
MySQL 5.0 alpha (binary install) on Solaris 9 -or- RedHat 8.0 mysql-connector-java-3.0.8-stable ----------------------- Is there something different going on with JDBC and the alpha version...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
0
by: Peter Höltschi | last post by:
I try to install MySQL Connection/J on a Windows XP box. MySQL on localhost works fine. But I cannot load the MySQL Connection/J Driver. The CLASSPATH variable is set like this:...
33
by: Joshua D. Drake | last post by:
Hello, I think the below just about says it all: http://www.commandprompt.com/images/mammoth_versus_dolphin_500.jpg Sincerely, Joshua Drake
2
by: KaHuNa | last post by:
this code works perfectly when i use it on the server where mySQL is installed, but i have a security error when i use it on a client computer. I don't know how i can change that. using...
7
by: Paul berry | last post by:
I have a project I'm working on where I just won't be able to have SQL enterprise. It's an ASP.Net VB project, and it needs to scale. Can I just use rigid and relational XML and have the entire...
12
by: Martien van Wanrooij | last post by:
I have been using for a while a class to create photo albums. Shortly, an array of photo's is declared, initially they are shown as thumbnails, by clicking on a thumbnail a larger photo is shown...
6
by: fpcreator2000 | last post by:
Hello everyone. I'm having problems with a page I've created that is used to insert data into a database. It uploads two files into two distinct folder, and it takes the filenames and inserts...
1
by: cumupkid | last post by:
II am trying to create a form that will allow me to upload photos to a folder in the site root directory and add the information to the mysql db at the same time. I have created two forms, one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.