|
Manfred Stienstra
manfred.stienstra at dwerg.net
Thu Dec 18 11:29:39 EST 2003
On Thu, 2003-12-18 at 11:13, Giampiero Benvenuti wrote:
> import MySQLdb, MySQLdb.cursors, mod_python
>
> def readtitle(req):
> req.content_type = "text/html"
> req.send_http_header()
> mydb = MySQLdb.connect(db='S_N', user='giampiero', compress=1,
> cursorclass=MySQLdb.cursors.DictCursor)
> cursor = mydb.cursor()
> stmt = "SELECT news_title FROM news"
> cursor.execute(stmt)
> rows = cursor.fetchall()
> rows = list(rows)
> for row in rows:
> results = (row['news_title']+',<br>')
> req.write(results)
>
> raise mod_python.apache.SERVER_RETURN, mod_python.apache.OK
This:
> rows = cursor.fetchall()
> rows = list(rows)
> for row in rows:
> results = (row['news_title']+',<br>')
> req.write(results)
can be compressed to (and made faster):
results = []
for row in cursor.fetchall():
results.append(row['news_title'])
req.write("<br />".join(results))
Also using the DictCursor gives a little overhead in this case. This is
ofcourse not a real problem on small queries like this one.
Manfred
|