[mod_python] mod_python & MySQLdb question

Giampiero Benvenuti giampiero.benvenuti at chiaroscuro.com
Thu Dec 18 01:54:59 EST 2003


Thanks for your prompt answer and your help.

I'm coming from Zope, just got to mod_python a week ago. Please 
understand my being naïve.

I'm not quite there yet: thanks to your help now I'm able to get all my 
titles form mysql, but at the
end of the page (sigh) I get an apache error: "500 Internal Server 
Error"
"The server encountered an internal error or misconfiguration and was 
unable to complete your request..."

Apache is not OK at all.

here is my code:

import MySQLdb, MySQLdb.cursors
from mod_python import apache

def read(req):
	req.content_type = "text/html"
	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()
	mydb.close()
	for row in rows:
		s = row['news_title']
		print s
	return apache.OK

Thank you guys,

Giampiero





On Mercoledì, dic 17, 2003, at 18:40 Europe/Rome, 
<tpc at csua.berkeley.edu> wrote:

>
> hi Giampiero, instead of print or echo, you use return or req.write.  I
> had a similar question here:
>
> http://www.modpython.org/pipermail/mod_python/2003-June/003263.html
>
> I hope that helps you.
>
> On Wed, 17 Dec 2003, Giampiero Benvenuti wrote:
>
>> Hello to all of you,
>>
>> I'm quite intrigued by mod_python and the difference with "normal"
>> python cgi...
>>
>> for instance: I can't make this great pice of code (Jaroslaw Zabiello,
>> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/171463) wich
>> works great in my cgi-bin directory, working with mod_python.
>>
>> The problem I have is right at the end, when I have to deal with:
>>
>>   for row in rows:
>>      print row['name'], row['txt']
>>
>> The question is: how can I translate this last part of code to make it
>> working with mod_python?
>>
>> the great code:
>> ----------------------------------
>> import MySQLdb
>> import MySQLdb.cursors
>>
>> conn = MySQLdb.Connect(
>>      host='localhost', user='root',
>>      passwd='', db='test',compress=1,
>>      cursorclass=MySQLdb.cursors.DictCursor) # <- important
>> cursor = conn.cursor()
>> cursor.execute("SELECT name, sometext txt FROM foo")
>> rows = cursor.fetchall()
>> cursor.close()
>> conn.close()
>>
>> for row in rows:
>>      print row['name'], row['txt'] # bingo!
>> ------------------------------------
>>
>> any help is much appreciated.
>>
>> Thanks,
>>
>> Giampiero
>>
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>
>
>




More information about the Mod_python mailing list